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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:13:09+00:00 2026-05-23T16:13:09+00:00

Say I have base class A. it has method void foo(A* a); It also

  • 0

Say I have base class A. it has method

void foo(A* a);

It also has method

void foo(B* b);

B inherits from A.

Say I now have a B instance but it is an A* ex:

A* a = new B();

If I were to call

 someA.foo(a);

Would this call the A* implementation or B* implementation of the method?

I’m providing an A* to the method, but what that object actually is is a B().

Thanks

  • 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-23T16:13:10+00:00Added an answer on May 23, 2026 at 4:13 pm

    Well, two things will happen. First of all the determination of which function to call:

    A* a = new B();
    foo(a);
    

    Here you pass a variable of type A* (C++ = static typed, remember) to foo, this will as usual call foo(A* a), nothing different from any other function overloading. If you were to call foo(new B()) it would use the implicit type B* and end up calling foo(B* b). Nothing new here, plain old function overloading. Note that only when foo(B*) is not present it will fall back to a more generic version because of inheritance.

    Now in your example we come to the calling of this function:

    void foo(A* a)
    {
        a->foo();
    }
    

    Well, again, standard C++ calling conventions apply, including polymorphism. This means if you have declared foo as virtual the vtable will be constructed in such a way that the foo method of B will be called for your example (as the object is created as type B). If A::foo() is not declared as virtual, the method of A itself will be called.

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

Sidebar

Related Questions

Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
Lets say we have a derivided class SerializableLabel from the base class System.Windows.Controls. [XmlRoot(SerializableLabel)]
Say I have a base class TestBase where I define a virtual method TestMe()
Let's say I have a Base class and several Derived classes. Is there any
In VB.NET there is a keyword 'shadows'. Let's say I have a base class
Say I have a couple of basic objects like so: [Serializable] public class Base
Let's say I have the following class hierarchy in C++: class Base; class Derived1
Let's say we have these two classes: public class Base { public static int
Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,

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.