Inheritance Trouble

Z

zippy747

I would like to have a base class that does some work and can be
instantiated.

class a
{
public:
virtual int foo() { return 0; };
virtual int bar() { return 0; };
};

I would like to have a derived class that does some slightly different
work and can be instantiated.

class b : public a
{
public:
virtual int foo() { return 1; };
};

I would like to have another derived class that does some slightly
different work in a different area and can be instantiated.

class c : public a
{
public:
virtual int bar() { return 2; };
};

I would like to have another derived class that takes some
functionality from b and some from c:

class d : public b, public c
{

};

I would like the output of the following code

int main()
{
a theA;
printf("%i\n", theA.foo());
printf("%i\n", theA.bar());

b theB;
printf("%i\n", theB.foo());
printf("%i\n", theB.bar());

c theC;
printf("%i\n", theC.foo());
printf("%i\n", theC.bar());

d theD;
printf("%i\n", theD.foo());
printf("%i\n", theD.bar());

return 0;
}

to be:

0
0
1
0
0
2
1
2

Obviously, it isn't since theD doesn't know if it should use
foo()/bar() from the a->b->d inheritance tree or the a->c->d
inheritance tree.
What is the proper way to meet these requirements?

Thanks,
-zip
 
V

Victor Bazarov

zippy747 said:
I would like to have a base class that does some work and can be
instantiated.

[...]
What is the proper way to meet these requirements?

Read about virtual inheritance.

V
 
J

jeffc

Victor Bazarov said:
zippy747 said:
I would like to have a base class that does some work and can be
instantiated.

[...]
What is the proper way to meet these requirements?

Read about virtual inheritance.

I don't think that will solve his problem, because virtual inheritance relates
to state, and he is trying to define behavior, correct?
 
V

Victor Bazarov

jeffc said:
zippy747 said:
I would like to have a base class that does some work and can be
instantiated.

[...]
What is the proper way to meet these requirements?

Read about virtual inheritance.


I don't think that will solve his problem, because virtual inheritance relates
to state, and he is trying to define behavior, correct?

Probably. I didn't spend enough time to understand the OP's problem.
Perhaps it can be solved with casting the 'theD' object to 'b' or 'c'
or 'a', explicitly.

V
 
O

Olivier Azeau

zippy747 said:
I would like to have a base class that does some work and can be
instantiated.
[SNIP]

I would like to have another derived class that takes some
functionality from b and some from c:

class d : public b, public c
{

};
[SNIP]
Obviously, it isn't since theD doesn't know if it should use
foo()/bar() from the a->b->d inheritance tree or the a->c->d
inheritance tree.
What is the proper way to meet these requirements?

Maybe you can try using the "using" directive to specify which foo and
bar you want class d to use.
 
J

Jay Nabonne

I would like to have a base class that does some work and can be
instantiated.
I would like to have another derived class that takes some
functionality from b and some from c:

class d : public b, public c
{
public:
virtual int foo() { return b::foo(); }
virtual int bar() { return c::bar(); }

Works?

- Jay
 
J

jeffc

marieddu78 said:
class b : public virtual a

class c : public virtual a

This is virtual inheritance. How exactly does this solve the OP's problem?
Virtual inheritance exists to avoid making 2 copies of the base class. This
doesn't have anything to do with the fact that his D object doesn't know which
function to invoke.
 
O

Olivier Azeau

Jay said:
public:
virtual int foo() { return b::foo(); }
virtual int bar() { return c::bar(); }



Works?

- Jay

What about

class d : public b, public c
{
public:
using b::foo;
using c::bar;
};

?
 
J

Jay Nabonne

Jay Nabonne wrote:

What about

class d : public b, public c
{
public:
using b::foo;
using c::bar;
};

?

One question is: how should D behave when used in the context of a B (that
is, polymorphically)? That's one thing unclear from the OP. I don't think
the "using" strategy will affect polymorphic behavior. So the correct
approach is determined by the unstated needs... :)

- Jay
 

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

No members online now.

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top