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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:19:36+00:00 2026-05-27T19:19:36+00:00

Probably best explained with an example: struct A { virtual void foo() = 0;

  • 0

Probably best explained with an example:

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


struct B
{
    virtual void foo() = 0;
};


struct C : A, B
{
    // Implementation of A::foo
    void A::foo();

    // Implementation of B::foo
    void B::foo();
};


// Define here; howto?

I’m aware I can define the functions inline with the class however that’s not feasible in this instance (there are about 6 base classes to implement all with 40+ methods)

—

Apparently I wasn’t clear enough. The question directly is: How to define both foo() methods that have been inherited without causing ambiguity issues?

The following does indeed work

void C::foo() { /* etc */ } 

This will define the implementation of A::foo() yet what of B::foo() ? Bearing in mind I do not wish to define the methods within the declaration of C.

P.S., the reason for modelling the problem this way at all is predefined (COM/OLE)

—

This is fine (MSVC) albeit inline:

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

struct C : A, B
{
    int A::foo() { return 1; }
    int B::foo() { return 2; }
};


void main()
{
    C* p = new C();

    cout << ((A*) p)->foo();
    cout << ((B*) p)->foo();
}
  • 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-27T19:19:37+00:00Added an answer on May 27, 2026 at 7:19 pm

    You can’t provide separate overrides for two functions with the same signature defined in two base classes. (You can’t define them inline within the class either, as you seem to think).

    One possibility is to introduce intermediate base classes to rename the functions:

    struct ABodge : A
    {
        void foo() {A_foo();}
        virtual void A_foo() = 0;
    };
    
    struct BBodge : B
    {
        void foo() {B_foo();}
        virtual void B_foo() = 0;
    };
    
    struct C : ABodge, BBodge
    {
        void A_foo();
        void B_foo();
    };
    

    Alternatively, you could avoid multiple inheritance by composing two internal classes, each of which implement one of the base class interfaces:

    class C
    {
    public:
        // Could probably be defined as `operator A&()`, etc. to look more like
        // inheritance, but I usually prefer conversions to be explicit.
        A       & asA()       {return a;}
        A const & asA() const {return a;}
        B       & asB()       {return b;}
        B const & asB() const {return b;}
    
    private:
        // These might need back-references to the 'C' that contains them.
        struct AImpl : A {void foo();} a;
        struct BImpl : B {void foo();} b;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, probably best to give an example here of what I mean. Imagine a
This is probably best explained with an example. I have the following table, where
My question will probably be best explained by an example. For example, I have
This is probably best shown with an example. I have an enum with attributes:
This will probably be obvious but I can't find the best way. I want
This is probably best explained through some code. I know that in the following
This is probably best explained with some code: public IQueryable<DatabaseRecord> GetQueryableLinkedRecords() { if(this.currentlyHeldIds.Count() ==
Best to be explained at an example: I am on branch 0.58 of repository
working PHP probably on amazon ec2... can anyone explain what would be the best
Probably best to explain with an example: <div id=tab-1> <input name=1 type=text /> <input

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.