is it correct behavior of Virtual function ?

P

Pallav singh

Hi All ,

in the below code i have created derived class object and in base
class we have virtual function.
i am using object slicing to copy only base part of it........ why is
not VTable getting updated Properly

Since in derived object's base part have address of derived class
virtual function address
Why its Not getting copied to base part with object slicing

is is behaving properly ?

#include <iostream>
using namespace std;

class Base
{
public :
virtual void func()
{ cout<<" I am inside Base "<<endl; }
};

class derived : public Base
{
public :
void func()
{ cout<<" I am inside Derived "<<endl; }
};

int main()
{
derived obj;
Base b = obj;
b.func();
return 0;
}
I am inside Base


Thanks
Pallav Singh
 
J

Juha Nieminen

Pallav said:
derived obj;
Base b = obj;
b.func();

'b' is an object of type Base, and naturally Base::func() will be
called. It doesn't matter how 'b' has been initialized. Initialization
doesn't change its type.

There's a difference between an *object* of type Base and a *pointer*
to Base. A pointer doesn't fix the object's type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top