user441521
user441521

Reputation: 6998

C++ "tapping" into virtual functions of base classes

I think this will be a strange question but I'm wondering if it's at all possible. If I'm using a library that I have no control over and if it has an Entity class that has virtual functions and the library itself is creating these entities like Entity* e = new Entity(), is there a way I can derive a class from Entity, override the virtual functions and somehow create an object that would "tap" into the virtual functions if I had a list of entities that were created with Entity* e = new Entity()? Note that I have no control over that code so I can't change the creation of the original entity so that it's like Entity* e = new MyEntity().

Upvotes: 0

Views: 67

Answers (1)

Jasper
Jasper

Reputation: 6556

This is not possible with standard C++ constructions. You need to hack the vtable, with google I found the following hack which achieves this: http://www.codeproject.com/Articles/54238/Changing-an-Object-s-Polymorphic-Behavior-at-Runti . Of course you should be really careful doing this, I would not recommend using this.

Upvotes: 1

Related Questions