-5

It is said that vptr is created when an object is created then why the size of class having virtual function comes as 4 on 32 bit machine and 8 on 64 bit machine, Is that mean there is some hidden vptr for base class having some virtual function (which is also inherited in derived classes) if yes then what is use of that vptr?

2 Answers2

0

The size of a pointer is OS dependant. It's 32 bits (4 bytes) on a 32 machine and 8 bytes in a 64 bits machine.

If you ask for the size of a class, not of a pointer, then the compiler may adjust it for a multiple of 32/64 depending on OS type.

Ripi2
  • 7,031
  • 1
  • 17
  • 33
0

The C++ standard does not define that a vtable or vptr has to exist, it rather defines how polimorphism and dynamic binding shall behave. Though a vtable is a very common implementation of polimorphism in conjunction with dynamic binding, a vptr is not a first class language construct like a pointer, which you could access like a member or which is defined to behave as a pointer regarding referencing/dereferencing or size.

So the compiler would be free to use other implementations, and so you cannot predict the size of objects with virtual member functions.

In most implementations, however, a vptr might address the same memory size as ordinary pointers, so it is very likely that the size of the vptr is the same as the size of an ordinary pointer at that architecture, i.e. 4 on 32 bit systems, or 8 on 64 bit systems.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58