Questions tagged [vptr]

Reference for C++ Virtual Pointer.

94 questions
2
votes
2 answers

Virtual table pointers

I decided to find out how vtable is built realy.So I opened debugger and found out some weird thing. The node ptr contains a few vptr. I always thought that there was only one vptr per object. Could anybody explain to me what's going on here ? (I…
2
votes
2 answers

In C++, can we upcast an array and then try to put another subtype into it (inspired by Java ArrayStoreException)?

I have tried to see what would happen in C++ if we try to "break" an array of objects in a similar way we can try to do it in Java. In Java we can have an array of type Double[], for example, upcast it to Number[] (because Double is a subclass of…
1
vote
5 answers

reference variables and inheritence

The following code: #include class Parent { public: virtual void func() {printf("Parent\n");} }; class Child1 : public Parent { virtual void func() {printf("Child1\n");} }; class Child2 : public Parent { virtual void func()…
OSH
  • 2,847
  • 3
  • 25
  • 46
1
vote
2 answers

How does Base class pointer access the member variable (vptr) of Derived class?

Lets consider the following example #include class Base { public: virtual void foo() { std::cout << "Base::foo()" << std::endl; } }; class Derived : public Base { public: void foo() override { std::cout <<…
H Kumar
  • 37
  • 6
1
vote
2 answers

Virtual Table and Object Slicing

In object slicing, when a derived class object is copied to a Base class object, does the _vptr of Derived class also get copied to _vptr of Base class, like other members of class Base? If not, why? class Base { public : virtual void Display()…
1
vote
1 answer

c++ vtable in multiple inheritance, pointer to thunk method

I read this article: https://shaharmike.com/cpp/vtable-part2/ And I can not understand why in the vtable (at the end of article) we have this pointer: 0x400918 0x400820 non-virtual thunk to Child::FatherFoo() but not pointer directly to method…
1
vote
1 answer

How to find VPTR in C++ assembly code?

class Base { public: Base() {} virtual void Get() { } }; class Derivered : public Base { public: virtual void Get() { } }; int main() { Base* base = new Derivered(); base->Get(); return 0; } I use gcc 5.4.0 to compile the code, and…
Pengcheng
  • 439
  • 1
  • 4
  • 14
1
vote
3 answers

why do we even need VPTR?

And why don't we use the same method for non virtual functions? I mean, why do we use virtual functions in that way? Can't we just use them as non-virtaul ones and override them? And if this method is saving us time/space or what ever, why don't we…
GMan
  • 81
  • 7
1
vote
1 answer

Could not call virtual member function after read object from file

Problem: I wrote an object into a file in binary mode using std::fstream. However, when I read it back from that file to another object and then call one of the virtual member functions of this new object, there's a memory access violation error.…
Stoatman
  • 758
  • 3
  • 9
  • 22
1
vote
4 answers

memcpy derived class to base class, why still called base class function

I am reading Inside the C++ Object Model. In section 1.3 So, then, why is it that, given Bear b; ZooAnimal za = b; // ZooAnimal::rotate() invoked za.rotate(); the instance of rotate() invoked is the ZooAnimal instance and not that of Bear?…
Divlaker
  • 401
  • 6
  • 16
1
vote
3 answers

where is the overridden virtual method saved in the vtable c++ in multiple inheritance

In C++, there is no class representation at run-time but I can always call an overridden virtual method in the derived class. where is that overridden method saved in the vtable? here's a piece of code to demonstrate: struct B1 { virtual void f() {…
Loay
  • 483
  • 3
  • 18
1
vote
3 answers

Virtual functions mechanism implementation

first: I read somewhere that the mechanism of virtual functions is undefined. i.e. that mean that every compiler can impelement it differently. But, every text that I found about virtual function mechanisn talk about VTBL and VPTR. Is there another…
asaf app
  • 384
  • 2
  • 3
  • 12
1
vote
3 answers

Size of C++ object increases as I add more interfaces... does Java do this?

I'm doing a little experiment to try to mimic java's interfaces in C++. I have a class "Derived" inheriting from base class "Base" and also two interfaces. I notice that with each interface I inherit from, the size of my Derived class goes up,…
Verdagon
  • 2,456
  • 3
  • 22
  • 36
1
vote
3 answers

size of derived class in virtual inheritance

#include "stdafx.h" #include using namespace std; class ClassA { protected: int width, height; public: void set_values(int x, int y) { width = x; height = y; } }; class ClassB :…
nagaradderKantesh
  • 1,672
  • 4
  • 18
  • 30
0
votes
2 answers

When operator delete() in assembly deletes vptr pointer?

The vptr is deleted when operator delete() is called. But the vptr pointer is hidden, and we don't have to care about its memory structure (plus each compiler has a different system on how it works.) I'd like to know when exactly the vptr in an…
Dean Seo
  • 5,486
  • 3
  • 30
  • 49