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 963583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:41:37+00:00 2026-05-16T01:41:37+00:00

Say we have a class inheriting from two base classes (multiple inheritance). Base class

  • 0

Say we have a class inheriting from two base classes (multiple inheritance). Base class A is abstract, declaring a pure virtual function foo, the other base class B declares and implements a function foo of the very same signature.

struct A
{
  virtual void foo(int i) = 0;
};

struct B
{
  virtual void foo(int i) {}
};

struct C : public A, public B {};

I want to use the implementation of foo from base class B in my derived class C. However, if I do not implement the function foo a second time in my derived class C, I cannot instantiate any object of it (it remains abstract). Virtual inheritance does not help here as expected (class A and class B have no common base class).

I wonder if there is a way to “import” the implementation of foo from class B into class C in order not to have to repeat the same code.

Above example is of course contrived. The reason I want implement foo in class B is that I want to derive class D : public B and use class Bs implementation of foo. I know that inheritance is not (primarily) intended for code reuse, but I’d still like to use it in that way.

  • 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-05-16T01:41:38+00:00Added an answer on May 16, 2026 at 1:41 am

    In java, your sample code works. In C++ it doesn’t. A subtle difference between those languages.

    Your best option in C++ is to define C::foo() by forwarding to B::foo():

    struct C : public A, public B
    {
      virtual void foo(int i) { B::foo(i); }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary Let's say I have two C# 4.0 classes, one inheriting from the other:
Let's say I have a base class Animal from which a class Cow inherits,
I have two subclasess inheriting from the same abstract superclass. There is a common
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
I have an Abstract class, say Animal . From this class, I have many
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:
Let's say I have two classes: class A { public: A* Hello() { return
Say you have class A, which classes A1, A2, and A3 inherit from. All
Lets say I have: class X { function a() { echo Hello, ; }
Say I have classes class A{ //code for class A } class B{ //code

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.