473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

multiple inheritance and base pointer

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
5 3414
* 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
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
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
2
by: JAL | last post by:
Comments appreciated. I coded an approach to simulating multiple inheritance of implementation in C# using PIMPL and a type based static class factory that seems to combine the extensibility of...
3
by: ernesto | last post by:
Hi everybody I have the following class declarations: class Interface { public: virtual char* getName() const = 0; }; class BaseClass : public Interface {
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
2
by: Immortal Nephi | last post by:
You may have heard diamond shape. You create one base class. One base class has member functions and member variables. You create two derived classes. All member functions and member variables...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.