I am trying to implement a system similar to the one described here . That is, (ab) using the vtable modification to change the behavior of an object at runtime. This is part of my attempts to create an efficient type-type wrapper in the C ++ project I'm working on.
In the example, if you can’t access it, copy the vtable with the memcpy()pointer thisas such:
void setType( const DataType& newType )
{
memcpy( this, &newType, sizeof(DataType) );
}
However, I have a problem with this method: I do not have an object of the target class to copy vtable, and you do not want to create it, since some types are expensive to create.
Is there a way to access the vtable that will be placed in an object of this class without an object of this class?
It would be preferable if it were somewhat portable, but I pretty much came to terms with the fact that it was specific to the compiler; as such, the GCC / g ++ method would be acceptable if there is no other option. Suppose also that I am interested in building this on fairly standard OS and architectures.
I use C ++ 11 if that helps.
Edit: I want to be absolutely clear, I know how dangerous this behavior is. I am more interested in the idea and, perhaps, its narrow application in very controlled conditions than I am in it, it is a good idea for production software, despite what my introduction can offer.
user3995702
source
share