5

I have read about vtable and have understood the concept for base class pointers pointing to base and derived class objects. Can someone explain the case how vtable is created when both base class and derived class are objects and derived class object is assigned to base class object. Case 3 in the below example

#include <iostream>
#include <exception>
using namespace std;


class Base
{
public:
    virtual void function1() { cout<<"Base - func1"<<endl; }
    virtual void function2() { cout<<"Base - func2"<<endl; }
};

class Derived1: public Base
{
public:
    virtual void function1() { cout<<"Derived1 - func1"<<endl; }
};

class Derived2: public Base
{
public:
    virtual void function2() { cout<<"Derived2 - func2"<<endl; }
};

int main () 
{
    // Case 1
    Base* B1 = new Derived1();
    B1->function1();
    B1->function2();

    // Case 2
    cout<<endl;
    Base* B2 = new Derived2();
    B2->function1();
    B2->function2(); 

    // Case 3
    cout<<endl;
    Base B3;
    Derived1 D1;
    B3=D1;
    B3.function1();
    B3.function2(); 
}

output:

Derived1 - func1
Base - func2

Base - func1
Derived2 - func2

Base - func1
Base - func2
Sumanth V
  • 71
  • 1
  • 6
  • 1
    You have slicing for `B3`. – Jarod42 Jun 30 '15 at 09:12
  • 1
    Just pointing out that naming your classes D1, D2 and then your variables B1, B2, B3 can be confusing. At the very least use lower case for the variable names to make it apparent that they're not class names. Using certain conventions in your code can help you and whoever reads it. – aslg Jun 30 '15 at 09:16
  • 2
    You should not use the word "vtable" unless you are talking about an implementation that specifically uses the vtables technique to implement virtual functions. The C++ language itself doesn't have a notion of vtables. – n. m. could be an AI Jun 30 '15 at 09:18

2 Answers2

2

B3=Derived; is an example of object slicing... only the base class data members are assigned to, and the vtable pointer continues to point to the base class functions.

You should avoid slicing - it can be dangerous (see this answer for an explanation). Now you know the term, you'll easily find plenty of reading on object slicing....

Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0
Base B3;
D1 Derived;
B3=Derived;  //There is called default assignment operator

It is defined like that:

Base& operator=(const Base &objectToCopy) {...}

The operator takes an argument of type Base, so object of type D1 is treated as object of type Base. It means the assignment operator see only that fields of Derived which are in class Base. However, pointer to vtable (which technically is a hidden field) still is not copied, because it always create in constructor and it is permanently associated with the real object type.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • 1
    The operator takes an argument of type `const Base &`, which does *not* implicitly convert anything to `Base`. The conversion happens during the assignment, not during the call to the operator. – user207421 Jun 30 '15 at 10:30
  • It still has nothing to do with the argument type (which you have still mis-stated). It has to do with what happens inside the operator method. – user207421 Jun 30 '15 at 10:52
  • I don't understand, what you mean or you cavil at me now. I think my answer is clear enough. – Piotr Siupa Jun 30 '15 at 11:06