0

I have the below piece of code where I have a base class and a derived class. Both base class and derived class are having a function member sharing the same name. In the main(), I have typecasted a base class object to a derived class pointer and trying to call the function. To my utter surprise, it is calling the derived class function member. As far as I know, the base class object won't be having any information about the derived class object. So, how come my derived class pointer is still able to access the derived member function?

In the case of upcasting, I do understand derived class object will be having the contents of the base class that's why a base class pointer pointing to a derived class object will work as expected.

Can someone please help me in understanding how the derived class member function is getting called in this even when I am having a derived class pointer pointing to a base class object(which is having no information of derived class)?

#include<iostream>
using namespace std;


class base
{

public:
     void b()
    {
        cout << "base";
    }
};
class derived:public base
{
public:
    void b()
    {
        cout << "derived";
    }
};

int main()
{
    base b;
    derived * d1;
    d1 =static_cast<derived*>(&b);
    d1->b();        
    return 0;
}
amit singh
  • 385
  • 1
  • 3
  • 8
  • 1
    It's undefined behavior. You are "lucky" that it works _right now_, but it might also set your computer on fire at other times. – Max Langhof Mar 18 '19 at 08:57
  • 1
    d1 is a pointer to the derived class ..., b it's not virtual, so the compiler generate call to the Derived::b method. – Gojita Mar 18 '19 at 09:01

3 Answers3

1

In your specific case, it's perfectly normal that b is called.

You have a pointer to Derived class, b it's not virtual. So the compiler will generate a call to Derived::b method.

Now, when b will be executed, as you put crap in the this pointer, it's undefined behavior. But in your case, as you do not access the this pointer, there's no probleme.

Gojita
  • 490
  • 4
  • 10
0

Cast a base class to derived class results undefined behavior

Shannon
  • 54
  • 1
  • 11
0

As I know, the function doesn't takes any extra space in memory, it stores like the normal function. You can see a member function as a normal function with an extra this pointer. In general, which function to call is determined by the type of the object pointer. But the virtual function is different, it is called by virtual function table, in your code, there is no virtual function declared. So there is no virtual function table.

You can see your code in a different way:

void derived_b(derived* this)
{
    cout << "derived";
}
void base_b(base* this)
{
    cout << "base";
}
int main()
{
    base b;
    derived * d1;
    d1 =static_cast<derived*>(&b);
    derived_b(d1);        
    return 0;
}

If you define some member in class, and use it in function b, it may cause some error.like

class base
{

public:
    void b()
    {
        cout << "base";
    }
};
class derived:public base
{
    int a;
public:
    derived(){a = 1;}
    void b()
    {
        cout << "derived" << a;
    }
};

your code in main() may cause error because the object b don't have any member in memory.

Liu Yubo
  • 116
  • 3