-4

Why only default constructor only able to create vptr(Virtual Table Pointer) and vtable(Virtual Table)? Why parameter constructor not able to

  • 1
    *"Why only default constructor only able to create vptr(Virtual Table Pointer) and vtable(Virtual Table)?"* Where did you get that idea? That does not make any sense. – Baum mit Augen Oct 05 '16 at 08:33
  • constructors don't make virtual anythings. having a virtual function in a type makes the type polymorphic, but that's a compile-time thing and has nothing to do with what constructors do. – xaxxon Oct 05 '16 at 08:42

2 Answers2

5

First, vtables and vptrs are not specified by the C++ language standard.

They're an implementation detail, although as far as I know all extant C++ implementation use that technique to implement virtual function dispatch.

With such an implementation, all constructors for a class with virtual member functions, necessarily establish the object's vptr. Things wouldn't work without it. So …

Why parameter constructor not able to

… is simply an incorrect assumption.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Assuming the implementation uses vtables (a pretty common implementation choice, as noted by "Cheers and hth - Alf"), the creation/population of vtables and invoking constructors are distinct operations.

However, all instances of a given (non-abstract) class will - once constructed fully - have the same set of vtables (a class that inherits from multiple bases may have more than one vtable). There will certainly not be a different vtable depending on how the object is constructed (e.g. what parameters are passed to constructors, which constructor is invoked, etc).

Peter
  • 35,646
  • 4
  • 32
  • 74