Questions tagged [vptr]

Reference for C++ Virtual Pointer.

94 questions
0
votes
4 answers

Getting list of all existing vtables

In my application I have quite some void-pointers (this is because of historical reasons, application was originally written in pure C). In one of my modules I know that the void-pointers points to instances of classes that could inherit from a…
Patrick
  • 23,217
  • 12
  • 67
  • 130
0
votes
1 answer

C++ Polymorphism issues. Something to do with _vfptr

I am trying to create a program that has the user click twice on the screen and a rectangle is drawn according to what was clicked. Right now all I am trying to do is set my classes up to be able to correctly draw a rectangle manually without…
tysonsmiths
  • 485
  • 1
  • 8
  • 19
0
votes
2 answers

C++: Help understanding this line of code

I was looking for a way to access the vtable directly through a pointer and came across this post: http://www.codeproject.com/Tips/90875/Displaying-vtable-when-debugging It works fine and I can invoke functions through the vtable entries. But I'm…
madu
  • 5,232
  • 14
  • 56
  • 96
0
votes
2 answers

C++ child constructor and VPTR

In many sources, books etc. are written "don't call this->virtualFunction in child class constructor" and in some sources explain why you mustn't do it. Because at the moment of construction class isn't created at all. So virtual function that will…
Talkin
  • 3
  • 2
0
votes
1 answer

C++: How to look at vptr/ vtable contents

Every C++ object that has a virtual function has a vptr that points to a vtable. How can I see what this vptr is, and the contents it is point to? I understand this is compiler dependent and it could put vptr anywhere in the object memory space. But…
madu
  • 5,232
  • 14
  • 56
  • 96
0
votes
2 answers

Understanding output when virtual functions are called directly using vptr

I was going through the code which I got from somewhere to understand how vptr and vtable works. Following is the code with the output class Base1 { virtual void fun1() { cout<< "Base1::fun1()" << endl; } virtual void func1() { cout<<…
cbinder
  • 2,388
  • 2
  • 21
  • 36
0
votes
4 answers

Are there cases where a class declares virtual methods and the compiler does not need to use a vptr?

I was wondering if there is a possible optimization where the compiler does not need to assign a vptr to an instantiated object even though the object's type is a class with virtual methods. For example consider: #include struct…
zr.
  • 7,528
  • 11
  • 50
  • 84
0
votes
0 answers

C++ - set base class vptr without constructor?

I've written a memory manager in C++. The details aren't important, but when set up it allocates a large byte array from the heap, and when its allocate function is called, it clears some free memory bytes to 0 and returns a pointer to them. So an…
user2226001
  • 89
  • 1
  • 8
0
votes
2 answers

where is the vptr (virtual pointer) initialized in a class having only parameterized constructors?

Suppose I have a class like this class Base { private: int i; int j; public: Base(int i) { this->i = i; j = 0; } Base(int i, int j) { this->i = i; …
bibbsey
  • 969
  • 2
  • 9
  • 14
0
votes
2 answers

Existence of VPTR in subobjects

I have a class 'base' with a virtual destructor and thus a VTable and corresponding VTPR in it, and a class derived from it: class base { public: virtual ~base() {} }; class der : base {}; main() { int a = sizeof(base); // = 4 , fine ! …
cirronimbo
  • 909
  • 9
  • 19
0
votes
3 answers

C++: prototype of a virtual pointer

I am not sure if this is documented anywhere. We all know in case of virtual functions, each class holds a vptr which pointer to an array of function pointers called the virtual table. I want to know what is the prototype of the vptr. For ex, if a…
rahul
  • 6,447
  • 3
  • 31
  • 42
-1
votes
1 answer

Is there a way to decrease the size of the object by removing "vptr"

I have a code like this using CRTP and C++20: template class Base { public: void m() { static_cast(this)->feature(); } virtual constexpr void feature() = 0; } class BaseImpl: public…
-1
votes
1 answer

Why using a different return type in virtual function declaration throws an error instead of resulting in a redefinition?

Base class: class Base { public: virtual int f() const { return 1; } }; Derived class: class Derived: public Base { public: void f() const {} }; Above code throws a "return type is not…
-1
votes
1 answer

How a single vtable is tracking new virtual functions?

I'm using VS 2013 and trying to see how vptr and vftable are working at object level. So I have the following classes: #include using namespace std; class baseClass { public: void nonVirtualFunc() {} virtual void…
madu
  • 5,232
  • 14
  • 56
  • 96
-1
votes
3 answers

Is virtual table necessary for C++?

I have a doubt about C++ virtual table recently. Why does C++ use virtual table? =>Because C++ compiler does not know the actual function address --->Why? =>Because C++ compiler does not know the exact type(Cat? Dog? Animal?) of the object the…
Guocheng
  • 543
  • 3
  • 15