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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:36:24+00:00 2026-06-06T23:36:24+00:00

I have the following code sample: class A { public: A(int a):AA(a) {}; int

  • 0

I have the following code sample:

class A
{
public:
    A(int a):AA(a) {};

    int AA;

    virtual int Test() 
    {
        return AA;
    };
};

class B
{
public:
    B(int b):BB(b) {};

    int BB;

    virtual int Test() 
    {
        return BB;
    };
};

class C:public A, public B
{
public:
    C(int a, int b, int c) :A(a),B(b),CC(c) {};

    int CC;
};

int main()
{
    A *a = new C(1,2,3);
    B *b = new C(1,2,3);
    C *c = new C(1,2,3);


    int x = a->Test() ; // this is 1
    int y = b->Test() ; // this is 2
//  int z = c->Test() ; // this does not compile

    return 0;
}

I was expecting the calls to a->Test() and b->Test() to be ambiguous too as the object a is a C and therefore inherits from A and B both of whom have identical Test() functions. However, they both call the implementation which corresponds to the delcared type rather than the type that the object actually is.

Can anyone explain why these calls are not ambiguous?
Does C++ always behave this 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-06-06T23:36:26+00:00Added an answer on June 6, 2026 at 11:36 pm

    In fact,a C instance is both a full A instance and a full B instance (so holds a copy of A methods & B methods)

    Since a is a A* , the compiler will use the A virtual table copy that is inside of the C instance
    Since b is a B* , the compiler will use the B virtual table copy that is inside of the C
    instance

    you cannot use C* since the compiler will not now which Test() method of A or B you want to call (since the C class holds both A::Test & B::Test symbols)

    if you implement a C::Test() method, then it will be called both instead of A::Test() & B::Test() since method is virtual for both A & B.

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

Sidebar

Related Questions

I have the following simple code abstract class A { public abstract void Test(Int32
I have the following code in Java: public class ServerInfo { int serverId; int
In the following code sample I have an Async Calculator class. This is injected
I have the following code that doesn't compile. class Base { public: virtual ~Base()
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I am new to Emacs, and I have the following code as a sample.
I have an entity like so: public class Land { public virtual IDictionary<string, int>
I have the following sample code. class bar is derived from the base class
I have following class public class Value { ... public virtual Unit Unit {
I have been confused about ORM since I see the following sample code: public

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.