Questions tagged [vptr]

Reference for C++ Virtual Pointer.

94 questions
0
votes
0 answers

C++ runtime error invalid vptr when compiling with gcc 8.1 and gcc 8.2

I have a strange runtime error that only happens with GCC 8.2 and GCC 8.1. When attempting to delete a class with multiple inheritances, the compiler raises a runtime error related to a corrupted virtual pointer. This minimal example can be found in…
Oshio
  • 133
  • 4
  • 17
0
votes
0 answers

What is the meaning of a pointer to a virtual function?

I have such a piece of code: struct X { virtual void f(){} virtual void g(){} virtual void h(){} }; int main() { auto p = &X::f, q = &X::g, r = &X::h; X *x = new X; return 0; } I thought the pointer to a virtual function…
zclll
  • 77
  • 7
0
votes
2 answers

does pointer "this" in c++ support virtual mechanism?

Consider that: class B { void f() { vf(); }; virtual void vf(); }; class D: public B{ virtual void vf(); }; I thought that in C++ the implementation of B::f() is something like that: f(B *this) { *(this->vptr[index])(this); } Is…
0
votes
1 answer

C++ How do i get a pointer to a class' virtual function table?

Given: Example.h struct Base { virtual ~Def() = default; virtual void accept(struct DerivedVisitor* v) = 0; }; struct Derived : Base { int num; void accept(struct DerivedVisitor* v) override; }; struct DerivedVisitor { virtual…
Django
  • 57
  • 6
0
votes
1 answer

Why object with vptr is 12 bytes longer?

#include class B { public: virtual void f() {std::cout<<"HI";} int x; }; class A { public: void f() {std::cout<<"HI";} int x; }; int main () { A a; B b; std::cout<
SimpleAsk
  • 73
  • 5
0
votes
0 answers

Force a specific arrangement of virtual and non virtual base classes

If I have a code like this: class A { public: int data1; } class B { public: virtual void test() {} } class C : public A, public B { int data2; } and when I now create an instance of C its memory layout looks something like…
Panakotta00
  • 135
  • 9
0
votes
2 answers

Initializing objects with virtual functions on pre-allocated memory without placement new operator - is that possible? If not, why

Let's say there's a simple class hierarchy, and a state object that uses the derived class; struct base_class { int Value; base_class() { this->Value = 1; } virtual void Func() { printf("Base\n"); }; }; struct derived_class :…
Stas L
  • 5
  • 4
0
votes
2 answers

are vptr and vtable inherited from base class?

As can be seen, D3 introduces a new virtual function, @function3(), in the middle of the chain of inheritance. I am wondering what is happening with *--vptr and vtable when this happens. D3 is a 'kind of' a new base class now, class Base { public: …
meng
  • 3
  • 2
0
votes
1 answer

How vptr and vtable works in the bellow virtual related code?

As far i know when we make a function virtual in base class a pointer which can be referred as vptr is created by the compiler and a vtable which holds the entrys of virtual function which are latest version for this class in case of overridden…
user11712295
0
votes
2 answers

Number of vptr created if derived class also has a virtual function which is not present in Base class

class Base { public: virtual void func1() { std::cout<<"Base func1"<
Naresh
  • 155
  • 1
  • 9
0
votes
1 answer

placement new with derived class

C++ gurus. Need your help with this little head scratcher: #include struct B{ virtual ~B() = default; virtual void talk() { std::cout << "Be-e-e\n"; } }; struct D:B{ void talk() override { std::cout << "Duh\n"; } ~D() {…
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
0
votes
3 answers

Unexpected virtual function dispatch when using base class reference instead of pointer

Let say I have a simple class hierarchy as follows with a common api: #include class Base { public: void api() { foo(); } protected: virtual void foo() { std::cout << "Base" <<…
motam79
  • 3,542
  • 5
  • 34
  • 60
0
votes
3 answers

How many entries are in a vtable for an abstract virtual function?

I read that an abstract class can still have a table. But I'm confused on how many entries it would have in its vtable. For example, if my abstract class was: class Circle(){ virtual void draw() = 0; } then how many entries would be in its…
smith1453
  • 151
  • 1
  • 1
  • 6
0
votes
1 answer

How virtual inheritance solves Multiple inheritance(Diamond) in c++?Which path will it take?

Below Code is on Diamond problem. virtual inheritance solves this ambiguity. #include using namespace std; class A { public: void something(){cout<<"A"<
0
votes
2 answers

Object construction and Virtual pointer during mutiple inheritance

class Base1 { virtual void fun1() { cout << "Base1::fun1()" << endl; } virtual void func1() { cout << "Base1::func1()" << endl; } }; class Base2 { virtual void fun1() { cout << "Base2::fun1()" << endl; } virtual void func1() {…
anurag86
  • 1,635
  • 1
  • 16
  • 31