I am attempting to implement a system similar to the first described here. That is, the (ab)use of vtable modification to change object behavior at runtime. This is part of my attempts to create an efficient type-generic wrapper in a C++ project I am working on.
The example, should you be unable to access it, copies the vtable using memcpy()
and the this
pointer as such:
void setType( const DataType& newType )
{
memcpy( this, &newType, sizeof(DataType) );
}
However, I have an issue with this method: I do not have an object of the target class to copy the vtable from, and do not want to have to create one, as some types are costly to construct.
Is there a way to access the vtable which would be emplaced into an object of a given class without an object of that class?
It would be preferable if it were somewhat portable, but I have largely resigned to this being compiler-specific; as such, a GCC/G++ only method would be acceptable if there is no other option. Let us also assume I am only concerned with building this on fairly standard OSes and architectures.
I am using C++11, should that aid in this somehow.
Edit: I want to be completely clear, I know how dangerous this sort of behavior is. I'm more interested in the idea and perhaps its narrow application in very controlled circumstances than I am in it being a good idea for production software, despite what my intro might suggest.