Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:15:20+00:00 2026-06-08T21:15:20+00:00

I have never used multiple inheritance but while reading about it recently I started

  • 0

I have never used multiple inheritance but while reading about it recently I started to think about how I could use it practically within my code. When I use polymorphism normally I usually use it by creating new derived instances declared as base class pointers such as

 BaseClass* pObject = new DerivedClass();

so that I get the correct polymorphic behaviour when calling virtual functions on the derived class. In this way I can have collections of different polymorphic types that manage themselves with regards to behaviour through their virtual functions.

When considering using multiple inheritance, I was thinking about the same approach but how would I do this if I had the following hierarchy

 class A {
     virtual void foo() = 0;
 };
 class B : public A {
     virtual void foo() {
         // implementation
     }
 };

 class C {
     virtual void foo2() = 0;
 };
 class D : public C {
     virtual void foo2() {
         // implementation
     }
 };

 class E : public C, public B {
     virtual void foo() {
         // implementation
     }
     virtual void foo2() {
         // implementation
     }
 };

with this hierarchy, I could create a new instance of class E as

 A* myObject = new E();

or

 C* myObject = new E();

or

 E* myObject = new E();

but if I declare it as a A* then I will lose the polymorphism of the class C and D inheritance hierarchy. Similarly if I declare it as C* then I lose the class A and B polymorphism. If I declare it as E* then I cannot get the polymorphic behaviour in the way I usually do as the objects are not accessed through base class pointers.

So my question is what is the solution to this? Does C++ provide a mechanism that can get around these problems, or must the pointer types be cast back and forth between the base classes? Surely this is quite cumbersome as I could not directly do the following

 A* myA = new E();
 C* myC = dynamic_cast<C*>(myA);

because the cast would return a NULL pointer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:15:21+00:00Added an answer on June 8, 2026 at 9:15 pm

    With multiple inheritance, you have a single object that you can view any of multiple different ways. Consider, for example:

    class door { 
       virtual void open();
       virtual void close();
    };
    
    class wood { 
        virtual void burn();
        virtual void warp();
    };
    
    class wooden_door : public wood, public door {
        void open() { /* ... */ }
        void close() { /* ... */ }
        void burn() { /* ... */ }
        void warp() { /* ... */ }
    };
    

    Now, if we create a wooden_door object, we can pass it to a function that expects to work with (a reference or pointer to) a door object, or a function that expects to work with (again, a pointer or reference to) a wood object.

    It’s certainly true that multiple inheritance will not suddenly give functions that work with doors any new capability to work with wood (or vice versa) — but we don’t really expect that. What we expect is to be able to treat our wooden door as either a door than can open and close, or as a piece of wood that can burn or warp — and that’s exactly what we get.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used Perl, but I am really impressed by the ack ,
I have never used Try-catch in my code before, but now I need to
I am new ASP.NET and I have never used a GridView or DataGrid, but
I want to use mysql with a rails app (I have never used mysql
G'day, I have never really used excel formulas before but need to convert a
I have used Fancybox on multiple occasions and never run into this problem. I
I've never used the ConcurrentDictionary object before and have a couple questions about it:
I have never used JOINs or have worked with multiple table before. This error
I have never used Foxpro before. Can you convert the following Foxpro code into
I have never used DBIx::Class until today, so I'm completely new at it. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.