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

  • Home
  • SEARCH
  • 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 8194239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:45:19+00:00 2026-06-07T04:45:19+00:00

I have been studying c++ for an exam and I thought that i had

  • 0

I have been studying c++ for an exam and I thought that i had understood most of the c++ commons misconcemptions with much fatigue but i’ve encountered an exercise from a past exam that is driving me crazy, it combines virtual methods and inheritance in a way that i dont seem to understand here is the code:

    #include <iostream>

    class B;

class A {
    public:
    virtual A* set(A* a) = 0;
};

class B : public A {
    public:
    virtual A* set(B* b) {
            std::cout << "set1 has been called" << std::endl;
            b = this;
            return b;
    }

    virtual B* set(A* a) {
            std::cout << "set2 has been called" << std::endl;
            a = this;
            return this;
    }
};

int main(int argc, char *argv[]) {
    B *b = new B();
    A *a = b->set(b);
    a = b->set(a);
    a = a->set(b);
    a = a->set(a);
    return 0;
}

the output is

set1 has been called
set2 has been called
set2 has been called
set2 has been called

From what i’ve gathered the first call (b->set(b) ) calls the first method of class B and return b itself and then this objectref gets casted to A meaning that now the object b is now of type A?
so i have A *a = A *b;
now it makes sense to me that i should call set of A since i have this situation in my mind
objectoftypeA->set(objectoftypeA) so i m not supposed to look into virtual methods since the two object are base classes ?

Anyway as you can see I have much confusion so bear with me if i make stupid errors i would be glad if someone could explain whats going on this code,i tried to search the web but i find only small and easy example that dont cause troubles.

  • 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-07T04:45:21+00:00Added an answer on June 7, 2026 at 4:45 am

    Potatoswatter is right, but I think I have a bit “clearer” explanation. I think the OP is getting confused on what happens at run-time with dynamic type lookup versus compile-time, and when up-casting happens automatically, versus when it does not.

    First off, return type does NOT affect which overload is called. You probably know that, but it bears repeating. A return type mis-match will cause an error at compile-time, but not run-time, and does not affect which overload is called. Also it’s worth noting that as long as it is a compatible pointer type (in a hierarchy together) returning a pointer doesn’t ever “change” it. It is still the same pointer, unlike converting floats to ints, where there is an actual change.

    Now to go through the calls one-by-one. This is my understanding of the process, not necessarily what the standard, or what “really” happens.

    When you call b->set(b) the compiler (not run-time) goes “looking for a method named set with an argument of pointer to B” which it finds with the one that outputs set1. It’s virtual, so there’s code to check if the class points to anything lower, but there isn’t, so it just calls it, and returns the this pointer into a.

    Now you’re calling b->set(a). Again it’s the compiler that goes “does b have an overload that takes pointer to A?” Yes it does, so it calls the “set2” method. It’s the compiler that sees an A* and so the call is “determined” at that point. Even though the pointer points to an object that is of type B, the compiler doesn’t know that, or care. So it’s the compile-time types of the arguments that determine which overloaded method get taken. From that point on, where in the hierarchy the virtual gets taken is on the underlying type of the this pointer, but only downward.

    Here’s a different case though. Try this: b->set(dynamic_cast<B*>(a)) This should call the “set1” method, because the compiler is going to definitely have a pointer to B (even if it’s nullptr).

    Now the third case: a->set(b). What’s happening here is the compiler says “there is only one set method, so can the argument be up-cast or constructed to that type?” The answer is yes, as B is a child of A. So that cast happens transparantly, and the compiler calls the ABSTRACT dispatcher for the set method of the type A. This occurs at compile time before the “real” type of what a is pointer to. Then at run-time, the program “walks the virtual” and finds the lowest one, the B->set(A*) method that emits “set2”. The actual type of what the argument points to isn’t used, only the type to the left of the arrow operator, and that only determines how far down the hierarchy.

    And the fourth case is just the 3rd again. The type of the argument (the pointer, not whta is pointed to) is compatible, so it goes as before. If you want a dramatic demonstration of this, try this:

    a->set((A*)nullptr) // prints "set2 has been called"
    b->set((A*)nullptr) // prints "set2 has been called"
    b->set((B*)nullptr) // prints "set1 has been called"
    

    The underlying type of what the arguments point to doesn’t affect dynamic dispatch. Only their “surface” type affects the overload called.

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

Sidebar

Related Questions

I've been studying because I have an exam and I don't have many problems
I have been studying as much as I can about the Module Pattern suggested
I have been studying the ECMAScript specification and have found that it is extremely
I have been studying the modalforms & inline formsets but am not able to
I have been studying Netty and Mina but am confused as to the best
I have been studying the database structure for shopping carts and notice that when
Have been studying the file system related classes of Adobe AIR 1.5, but so
been working on an app and have been studying the questions available here but
I am trying to teach ASP.NET MVC to students (undergrads) that have been studying
I have been studying python for quite sometime now and very recently I decided

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.