1

I have the following code, and I don't understand why it calls the A class function instead of B class function. Could someone tell me why??

#include<iostream>
using namespace std;
class A{
public:
    virtual void f(int n){
        f(n);
        cout << "A";
    }
};
class B :public A{
public:
    virtual void f(float f){
        cout << "B";
    }
};
int main(){
    A*p= new B;

    p->f(5.1);



}
Michael Albers
  • 3,721
  • 3
  • 21
  • 32
Mr. Hello_world
  • 85
  • 2
  • 10

2 Answers2

1

These are completely different functions. A function is identified by its name and its arguments. You have no overriding here: you have two distinct functions.

If you'd used the override keyword, the compiler would have immediately told you this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

I Modified your Code slightly as under. And I get the desired result. i.e.: when accessing a derived class reference from a base class pointer I get the compiler to map my function call correctly to the derived class' implementation.

I guess the following facts apply:

  1. A derived class would have to first implement(override: which doesn't say change the signature) the Virtual functions defined by the base class to be able to call/access them polymorphically.
  2. After a base class has implemented the virtual functions they can very well be overloaded.
  3. One other thing, If we see the VTable creation mechanism, Only when the derived class implements a virtual function defined at a base calss an entry for the same function name would be made. Otherwise the pointer A is still a map of the Class A and has no idea how to resolve the call to and ends up in implicitly casting the 5.1 double to int according to the implementation of function f in class A. this is pretty much what I mentioned in point#1
  4. virtual functions and pure virtual functions are a means of providing/creating an interface which can be shared across different layers of your software, where the you simply share the interface and the can hide the implementation from the user. So having same function name/ interfaces defined by two classes would only cause more confusion.

    #include<iostream>
    using namespace std;
    class A{
        public:
          virtual void f(int n){ cout << "A" << endl; }
    };
    
    class B :public A{
        public:
          void f(int f){ cout << "B" << endl;   }
          void f(float f){ cout << "B" << endl; }
    };
    int main(){
        A*p= new B;
        p->f(5.1);
    }
    

Since there are lot of pros in this forum, ff there is anything incorrect in my answer, please put in your comments. Thanks.

  • I have no idea what you're saying here. – Lightness Races in Orbit Sep 12 '15 at 23:02
  • @LightnessRacesinOrbit: Which part of the answer? – abhishek chattopadhyay Sep 13 '15 at 05:58
  • @lightness: OK, for overloading to work, we need a functions in the same scope. Now we also wanted that the overloaded function be called polymorphically . To obtain that we first need to override the original function f() declared in the base class (this would create the instance of f() in b and add a vtable entry such that polymorphic calls are possible). Once that is done you'd be able to polymorphically call any overloaded instance of f(). – abhishek chattopadhyay Sep 16 '15 at 15:55