[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Combining functions with multiple inheritance?

Dasuraga

10/29/2008 4:22:00 PM

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?
3 Answers

Victor Bazarov

10/29/2008 5:08:00 PM

0

Dasuraga wrote:
> 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;

I believe you meant

virtual void f() = 0;

> }
; // missing semicolon

> class B:virtual public A{
> void f();
> }
; // missing semicolon

> class C:virtual public A{
> void f();
> }
; // missing semicolon

> class D:virtual public A{
> void f();
> }
; // missing semicolon

> 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?

What do you mean by "automatically"? Have you tried "manually"? As in

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

void F::f() {
B::f();
D::f();
}

....

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Dasuraga

10/29/2008 5:33:00 PM

0

On 29 Oct, 18:08, Victor Bazarov <v.Abaza...@comAcast.net> wrote:


> 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.

Salt_Peter

10/29/2008 9:20:00 PM

0

On Oct 29, 12:32 pm, Dasuraga <Dasur...@gmail.com> wrote:
> On 29 Oct, 18:08, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
> > 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.

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.