Questions tagged [vptr]

Reference for C++ Virtual Pointer.

94 questions
4
votes
4 answers

When is the v-table created for a class?

I know that, how to implement virtual function call resolution is not part of C++ standrads nor it says anything about vptr or v-table, but let me ask this question here. I've heard that v-table is a common technique used by compilers to implement…
riderchap
  • 667
  • 2
  • 11
  • 19
3
votes
3 answers

Virtual inheritance and empty vtable in base class

There is this code: #include class Base { int x; }; class Derived : virtual public Base { int y; }; int main() { std::cout << sizeof(Derived) << std::endl; // prints 12 return 0; } I have read that when some class is…
scdmb
  • 15,091
  • 21
  • 85
  • 128
3
votes
2 answers

Virtual class inheritance object size issue

Here, in this code, the size of ob1 is 16 which is fine(because of the virtual pointer) but I can't understand why the size of ob2 is 24. #include using namespace std; class A { int x; }; class B { int y, z; }; class C : virtual…
3
votes
1 answer

Changing dynamic type of an object in C++

In the following question one of the answers suggested that the dynamic type of an object cannot change: When may the dynamic type of a referred to object change? However, I've heard that it is not true from some speaker on CPPCon or some other…
3
votes
2 answers

Does the size of vptr on 64-bit machines **has** to be 64 bits?

I'm curious why the size of vptr seems to take 64 bits on 64 bit machines and whether C++ actually require that. All vptr need to do is to point to vtables, and since vtables cannot take too much memory and can be grouped together 32 bits ought to…
Michael
  • 5,775
  • 2
  • 34
  • 53
3
votes
1 answer

Using C++ struct with virtual members (i.e. non-POD) in C

In questions such as this, compatibility between C++ classes/structs and C structs is explained as possible as long as all members are of the same type, in the same order, and no virtual members are declared. That's my problem. I have virtual…
Chris Watts
  • 6,197
  • 7
  • 49
  • 98
3
votes
2 answers

choosing vptr in case of multiple inheritance

This is similar to many previous questions, but it asks something which I was not able to find answer. #include using namespace std; class Base1 { public: int b1_data; virtual void b1_fn() {cout << "I am…
Vishal Sahu
  • 650
  • 12
  • 23
3
votes
2 answers

pointer to access member function through virtual pointer

I came across articles where in they explain about vptr and vtable. I know that the first pointer in an object in case of a class with virtual functions stored, is a vptr to vtable and vtable's array entries are pointers to the function in the same…
anurag86
  • 1,635
  • 1
  • 16
  • 31
2
votes
3 answers

Resolving of vptr

class base { public: virtual void fn(){} }; class der : public base {}; I know that compiler provides a member call VPTR in class which is initialised with the exact VTABLE at run time by constructor. I have 2 questions 1) Which class holds…
user966379
  • 2,823
  • 3
  • 24
  • 30
2
votes
4 answers

C++ Derived polymorphic class - does it contain an entire instance of Base, including a vptr?

Say we have Class A { public: int _i; virtual int getI(); }; class B : public A { public: int _j; virtual int getI(); }; So assuming that the size of a class in memory is the sum of its members (i.e. ignoring padding or whatever…
azphare
  • 497
  • 4
  • 11
2
votes
1 answer

Storage layout of polymorphic objects according to C++ standard

I know that if a class contains any virtual functions, most compilers (if not all) add a vptr pointer to its objects. Some add it as the first element, some as the last. But does the C++ standard mandate usage of vptr and vtable? Could any compiler,…
NPS
  • 6,003
  • 11
  • 53
  • 90
2
votes
2 answers

Can derived classes have more than one pointer to a virtual table?

I am watching the BackToBasics talk: Virtual Dispatch and Its Alternatives from CppCon2019. The presenter says and the slide shows (assuming I haven't misunderstood) that a derived class inherits a vtable pointer from the base class and…
CanISleepYet
  • 107
  • 6
2
votes
2 answers

Figuring out vptr field

I have a few classes and I'm trying to understand how the vptr and vtable work in this situation. class RGB { short value[3]; }; class AbstractImage{ protected: int n_pixels; public: virtual void show() = 0; virtual…
Adam Morad
  • 167
  • 1
  • 7
2
votes
1 answer

Why are classes with virtual functions aligned differently than classes without?

Inspired by this cppcon talk by Richard Powell I have created the following code snippet to fool around: #include using std::cout; using std::endl; struct erdos { void who() { cout << "erdos" << endl; } float f1; float…
Patryk
  • 22,602
  • 44
  • 128
  • 244
2
votes
2 answers

Determine the size of object without its virtual table pointers

Is there a generic way (not platform dependent) to get at compile time the size of a class object in the memory, without counting the vtable pointers?
Reflection
  • 1,936
  • 3
  • 21
  • 39