Combining functions with multiple inheritance?

D

Dasuraga

I'm rather new to the object-oriented aspects of C++, and this is the
first time I've had to construct some classes with multiple
inheritance. I'd rather not make a variable just to identify what type
an object is (leading to using a case, which somewhat defeats the
utility of derived classes). So, here's my problem. I have a couple
classes in this layout(simplistically):
class A{
void f()=0;
}
class B:virtual public A{
void f();
}
class C:virtual public A{
void f();
}
class D:virtual public A{
void f();
}
class E:virtual public B,virtual public C{};
class F:virtual public B,virtual public D{};

So, is there a way to make it so that E::f() automatically invokes
B::f(), then C::f()( similarly for F::f())? If not, is there a better
layout that you might be able to suggest?
 
D

Dasuraga

What do you mean by "automatically"?
I guess what I wanted was similar functionality as constructors, but I
guess that doing it "manually" works too, I just wanted to verify
if it was possible or not.
 
S

Salt_Peter

I guess what I wanted was similar functionality as constructors, but I
guess that doing it "manually" works too, I just wanted to verify
if it was possible or not.

It is possible, nn your case:

class E: public B, public C
{
public:
void f();
};

void E::f()
{
B::f();
C::f();
}

the calls to f() become also automatic. The last thing you want is the
language impose a restriction that says that all virtual overides are
implicitly called down the hierarchy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top