multiple inheritance and base pointer

S

Scott

Hi All,

Am I correct in assuming that there is no way to have a base pointer to an
object that uses multiple inheritance?

For example,

class A { /* ... */ };

class B { /* ... */ };

class C : public A, public B { /* ... */ };

class D : public A, public B { /* ... */ };

Is there a way to define a pointer that will allow access to objects of
either C or D with a dynamic_cast?

Thanks for any help,
Scott
 
A

Alf P. Steinbach

* Scott:
Am I correct in assuming that there is no way to have a base pointer to an
object that uses multiple inheritance?
No.


For example,

class A { /* ... */ };

class B { /* ... */ };

class C : public A, public B { /* ... */ };

class D : public A, public B { /* ... */ };

Is there a way to define a pointer that will allow access to objects of
either C or D with a dynamic_cast?

Yes.

But exactly what is the problem? See the FAQ item about how to post a
question about code that doesn't work.
 
S

Scott

Hi Alf,

Yes.

But exactly what is the problem? See the FAQ item about how to post a
question about code that doesn't work.

Thanks for the reply, and sorry about the vagueness of my post.
Essentially, I have a variety of classes that use multiple inheritance
(all from the same two classes), and I would like to be able to pass
pointers to any of them to another class. So, like my earlier example:

class A { ... };
class B { ... };
class C : public A, public B { ... };
class D : public A, public B { ... };

class E
{
E( ptr to either C or D here! )
{ ... }
};

Essentially class E is a container class for holding hash maps to objects
of either type C or D, or ultimately, any object that multiply inherits
from A and B. How do I define a base class pointer to objects C and
D. Also, I have no control over classes A, B, C or D, only E, so I cannot
change their definitions.

Hope this is better, and thanks again,
Scott
 
J

Jerry Coffin

Hi All,

Am I correct in assuming that there is no way to have a base pointer to an
object that uses multiple inheritance?
No.

For example,

[ code elided ... ]
Is there a way to define a pointer that will allow access to objects of
either C or D with a dynamic_cast?

I'm not sure I follow. A pointer to a base class can point to an object
of a derived class (any class derived from that base). The derived class
may have other base classes as well, without affecting this -- and no
dynamic_cast is needed for this. A pointer to a derived class can be
converted to a pointer to a (public) base class implicitly.

You use dyamic_cast to conversions on the opposite direction, starting
with a pointer to a base, and converting to a pointer to the derived
class. Again, multiple inheritance doesn't really affect much -- you can
have a pointer to any base class, and as long as the object really is of
the desired destination class, dynamic_cast can convert it successfully:

#include <iostream>

// switched to stucts since everything's public anyway.
struct A {
virtual void f() {std::cout << "A::f()\n"; }
};

struct B {
virtual void g() {std::cout<< "B::g()\n"; }
};

struct C : A, B {
virtual void f() { std::cout << "C::f()\n"; }
virtual void g() { std::cout << "C::g()\n"; }
void h() { std::cout << "h (unique to C)\n"; }
};

struct D : A, B {
virtual void f() { std::cout << "D::f()\n"; }
virtual void g() { std::cout << "D::g()\n"; }
void e() { std::cout << "e (unique to D)\n"; }
};

int main() {
// pointers to base classes pointing at objects of derived classes
A *a = new C;
B *b = new D;

// use the virtual functions.
a->f();
b->g();

// convert to pointers to derived objects
C *c = dynamic_cast<C*>(a);
D *d = dynamic_cast<D*>(b);

// conversions should have succeeded.
assert(c!=NULL);
assert(d!=NULL);

// invoke functions unique to derived classes.
c->h();
d->e();

// implicitly convert back to base class pointers. Note
// we've swapped the two this time.
a = d;
b = c;

// invoke virtual functions again. Note the changed output.
a->f();
b->g();

return 0;
}
 
A

Alf P. Steinbach

* Scott:
Essentially, I have a variety of classes that use multiple inheritance
(all from the same two classes), and I would like to be able to pass
pointers to any of them to another class. So, like my earlier example:

class A { ... };
class B { ... };
class C : public A, public B { ... };
class D : public A, public B { ... };

class E
{
E( ptr to either C or D here! )
{ ... }
};

Essentially class E is a container class for holding hash maps to objects
of either type C or D, or ultimately, any object that multiply inherits
from A and B. How do I define a base class pointer to objects C and
D. Also, I have no control over classes A, B, C or D, only E, so I cannot
change their definitions.

If A is a polymorphic class, just use a pointer to A. Likewise, you
could use B. If neither is polymorphic, create a wrapper class
interface I and a concrete wrapper class WC and a concrete wrapper class
WD (could be templated as a single class W), and use pointer to I.
 
S

Scott

I'm not sure I follow. A pointer to a base class can point to an object
of a derived class (any class derived from that base). The derived class
may have other base classes as well, without affecting this -- and no
dynamic_cast is needed for this. A pointer to a derived class can be
converted to a pointer to a (public) base class implicitly.

Thanks for the replies! A is polymorphic, so it seems I can just use
a pointer to that. I guess a simple answer to a confusing question
;-) I will try to be clearer in the future.

Thanks again,
Scott
 

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
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top