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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:15:00+00:00 2026-06-16T16:15:00+00:00

Suppose there is this interface: class A{ public: virtual foo()=0; }; And a class

  • 0

Suppose there is this interface:

class A{  
 public:  
  virtual foo()=0;  
};

And a class B which implements this interface:

class B:public A{    
 public:   
  virtual foo(){} //Foo implemented by B   
}

Finally, a class C which has classes A and B as base classes:

Class C : public A, public B {
};

My question is, there is a way to tell the compiler that the implementation for foo is the one from class B without doing an explicit call to B::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-06-16T16:15:02+00:00Added an answer on June 16, 2026 at 4:15 pm

    As @BenVoigt pointed out in the comments, the below answer only works due to a bug in g++ (meaning it isn’t guaranteed to keep working, and it definitely isn’t portable). Thus, although it may do what you want if you use a particular (buggy) compiler, it isn’t an option you should use.

    Do use virtual inheritance though.


    This isn’t exactly the scenario that the code in the question implies, but the sentence

    My question is, there is a way to tell the compiler that the
    implementation for foo is the one from class B without doing an
    explicit call to B::foo()?

    seems to be asking for syntax to distinguish between multiple base versions of a function without using the :: qualifier.

    You can do this with the using directive:

    #include <iostream>
    class A {
    public:
    A(){}
    virtual void foo(){std::cout<<"A func";}
    };
    
    class B: virtual public A {
      public:
      B(){}
      virtual void foo(){std::cout<<"B func";}
    };
    class C:virtual public A, virtual public B {
        public:
        C(){}
        using A::foo; // tells the compiler which version to use
                       // could also say using B::foo, though this is unnecessary
    };
    
    int main() {
        C c;
        c.foo(); // prints "A func"
        return 0;
    }
    

    Of course, the code itself in the question doesn’t require this at all, as the other answers have pointed out.

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

Sidebar

Related Questions

Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have this annotation class @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodXY { public int
Suppose I have: interface Foo { void doStuff(); } class FooImpl implements Foo {
There exists interface Algorithm There exists class MathAlgorythm implements Algorithm (returns MathResult, which implements
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Suppose there is a System A which gives some output. This output is used
Suppose I have: Foo foo; is there a shorthand for this? foo.operator->().operator()(1, 2);
Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots;
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
Suppose I have an empty interface class IBaseInterface which is used only to label

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.