473,769 Members | 6,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3454
* 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.c om>,
sc***@nospam.co m 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
8104
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 how and why it works. I'll post a simplified version of it below and ask my questions afterwards: class Base { void *function_ptr;
6
2840
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
23384
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 examples?
2
1247
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 inheritance with the encapsulation of containment. Hopefully, the C++/cli coders here can read this C# code. http://www.geocities.com/jeff_louie/oop27.htm
3
2656
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
2554
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 used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
47
4034
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
4
3961
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
2
2099
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 from one base class are inherited into two derived classes. You want both derived classes to share member variables of the one base class. You can do this way so you don't need keyword -- friend. You can add virtual public One_Base_Class on...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.