Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically changing the virtual pointer on runtime

Tags:

c++

vptr

So let's say I have two classes that inherit a base class that has a pure virtual function. Both of the classes implement their own version of that function, but don't add additional member variables so they have the same size. Now sometimes, in the middle of the execution of the program, I want to convert one class to the other without copying all its data. So basically I want to make it use the virtual table of the other class. Is there a portable way of doing this?

like image 589
slartibartfast Avatar asked Nov 30 '22 15:11

slartibartfast


1 Answers

The portable way to do this would be to implement your own class system that actually has virtual-pointers that can be copied.

There is no such thing as a virtual-pointer in standard C++.

like image 90
Mankarse Avatar answered Dec 15 '22 12:12

Mankarse