By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,825 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,825 IT Pros & Developers. It's quick & easy.

multiple inheritance and base pointer

P: n/a
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
Aug 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 8 '06 #2

P: n/a
Hi Alf,

On Tue, 08 Aug 2006 05:05:20 +0200, Alf P. Steinbach wrote:
>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.
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
Aug 8 '06 #3

P: n/a
In article <jY*********************@fe05.news.easynews.com> ,
sc***@nospam.com says...
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;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 8 '06 #4

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 8 '06 #5

P: n/a
On Mon, 7 Aug 2006 21:33:00 -0600, Jerry Coffin <jc*****@taeus.com>
wrote:
>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
Aug 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.