Diamond inheritance =/

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I have something like this:
Code:
class Base {
    public:
        virtual void doSomething ();
};

class Derived_Abstract {
    public:
        virtual void doSomething ();
        virtual void abstractStuff () = 0;
};

class Derived {
    public:
        virtual void doSomething ();
};

class OhMyGawdWhatAmIDoing : public Derived_Abstract, public Derived {
    public:
        virtual void doSomething ();
        virtual void abstractStuff () = 0;
};

The problem is class "OhMyGawdWhatAmIDoing" (It, too is abstract).
It implements the pure virtual method and it's doSomething() implementation calls the doSomething() method of both Derived_Abstract and Derived.

The thing is, it'll inherit Derived_Abstract's doSomething() implementation by default (something about dominance; some compiler warning). This generates a warning and I don't like to see warnings =/

My current solution is ugly but it gets rid of the warning:
Code:
class OhMyGawdWhatAmIDoing : public Derived_Abstract {
    public:
        virtual void doSomething ();
        virtual void abstractStuff () = 0;
    protected:
        Derived d; //Tadah
};
So, no more diamond inheritance and doSomething() can call the doSomething() method of both classes.
But it seems kinda' ugly, I duno.

Any way around this?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
You can call both functions by their qualified names:

Code:
class OhMyGawdWhatAmIDoing : public Derived_Abstract, public Derived {
    public:

		void doSomething (){
			Derived::doSomething();  //calls Derived's implementation
			Derived_Abstract::doSomething(); //calls Derived_Abstract's implementation
		};

		void abstractStuff (){};
};

But VS2010 doesn't give me any warnings at /W4.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Strange..
I don't get the dominance issue anymore.

Anyway,
That "OhMyGawdWhatAmIDoing" class is abstract because it's to be inherited from with the derived class implementing abstractStuff().

So, there are, like, multiple classes that derive from "OhMyGawdWhatAmIDoing" and when I have to convert a pointer from "DerivesFromOMG" to "Base", it becomes an ambiguous conversion =/

Code:
DerivesFromOMG omg;
Base* base_ptr = &omg;//Ambiguous conversion; can go the route of Derived_Abstract or Derived

Also, "Base" has a non-virtual method that when called from "DerivesFromOMG" is also an ambiguous call =/

I know I can use their qualified names but that I'll have two names that call the same method:
Derived_Abstract::nonVirtualMethod();
Derived::nonVirtualMethod();
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Ah, I get it.

You can use virtual inheritance.

However your example code is pretty messed up (no class derives from Base, undeclared functions etc) so I'll mash together a minimal-code example:

Code:
class Base {
    public:
		virtual void doSomething (){ /* blub */ };
};

class A : virtual public Base{ //<-- virtual
    public:
		virtual void doSomething () { /* blah */ };
};

class B : virtual public Base{ // <-- virtual
    public:
		virtual void doSomething () = 0;
};

class D : public A, public B { //you can derive from this one as much as you want to
    public:
		virtual void doSomething (){};
};


void main(){
    Base* base_ptr = new D(); // OK
}
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
This is strange...
I tried virtual inheritance earlier on but had problems but now that you're telling me to do it there isn't any..
I must have done something wrong earlier on =x

Thanks for the help s3rius!
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
When you have a class a inheriting from pure virtual class b and implementing its methods (non-virtually), then you have class c inheriting from class a and implementing its own version of all virtual functions from class b it will over-ride the functions from class a. Basically class a does not have to have the virtual modifier for class c to inherit it's functions virtually, and actually should not have the virtual modifier as it actually does nothing at that point unless it is an overloaded function or one that is not inherited.

While this works it is not recommended to use virtual functions in the end result application as they take up extra memory (virtual functions are actually function pointer members that are set based on the class they are created for, allowing pointers to these used as pointer from the base class to still call the derived function), and there are ways around virtual inheritance. They do have their uses (ease of use primarily) but larger applications using virtual functions can be very taxing on ram.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Yes, but I my actual usage at that point in time was for a game state class.

class GameState; <-- Base class
class MenuState; <-- Generic menu state
class StarsState; <-- A state that does nothing more than blit stars onto the screen and have them move
class MainMenuState; <-- Inherits from MenuState and StarsState

And other game state classes could just inherit from only MenuState or only StarsState and not over-ride the functions; so, I had a reason to make them inherit functions from class GameState virtually.

Thanks for the warning on virtuals, though.
I seem to forget that little extra cost every time.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
thats odd... generally you can overload dual inheritance from the same tree, at least iv never run into problems with it
ie:
A class a has the method: void DoAStuff ()
classes b and c inherit from class a
class d inherits from b and c
class d gets DoAStuff directly from a, then checks for overloads in b and c
I believe that the override inherited (if there are 2) would be the first one in the list of inherited classes (ie: class a : public b, public c... would inherit from b im guessing)
also class b and c's overrides can be called directly via:
Code:
this->b::DoAStuff()
Now where there might be an issue is if they both have functions named the same thing taking the same parameters, that would cause an error if i am correct, so that should actually be removed and added to class a
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Yeah, that works but the syntax isn't so pretty when we have classes with more complex names =x

Code:
class Base {
    public:
        void die () {}
};
 
class D1 : public Base {
};
 
class D2 : public Base {
};
 
class Error : public D1, public D2 {
};
 
int main () {
    Error e;
    e.die(); //Error; ambiguous
    return 0;
}
It just seemed ugly to have to qualify the call with a D1 or D2 when both call the same thing.

It isn't a matter of it being possible but which is simply cleaner =/
D1::die() and D2::die() mean the same thing but by allowing the option to choose one or the other, I risk making things confusing and being inconsistent by using D1::die() sometimes and D2::die() at others which was pretty bad (to me).
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
you can over-ride the die() function to manage which function it actually calls, so its not ambiguous
Code:
class Error : public D1, public D2
{
    void die () { D1::die(); }
}
 
int main ()
{
    Error e;
    e.die(); //No Error, not ambiguous
    return 0;
}

I see no reason for that to be ambiguous when they are actually inheriting the same exact function, that's just bad compiler programming, that should be added into the C++ strictures.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
IIRC, inheriting from both D1 and D2 creates two instances of class Base.
So, while D1 and D2 may provide extra functionality, they also have their own instances of class Base.

So, if class Base happens to have an integer "i" and we go:
Error e;
e.i = 5; //Error; also ambiguous

If I followed your suggestion, I'd have to override a lot of stuff if my class is particularly hairy and D1's methods would just operate on it's own Base while D2's methods would also operate on it's own Base.

The virtual inheritance was really all I needed because it creates only one instance; C++'s way of combating the problem, I suppose.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    So what it really is me trying to implement some kind of better site navigation not change the whole theme of the site
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top