Original Post
I'm seeking to expand my C++ education into the little nooks and crannies that have remained out of sight till now... Is there any way of setting up a specific virtual function table on an arbitrary block of memory without using 'new'? Example: Is there anything analogous to the 'deploy_vftable_cast' that I've used above? With or without RTTI? I thought of using a templated approach and copying the vftable from an instance of the CInheritedObj but I don't really relish this as an option... C++ gurus?
class CBaseObj
{
public:
virtual void Blah() = 0;
};
class CInheritedObj : public CBaseObj
{
public:
virtual void Blah()
{
printf("inherited");
}
};
void main()
{
void *pvMem;
CBaseObj *psObj;
// memory may be alloc'd with new/malloc/whatever.
pvMem = ObtainEnoughMemoryByArbitraryMeans(sizeof(CInheritedObj));
// clear this just to demonstrate that the vftable was not setup beforehand
memset(pvMem, 0, sizeof(CInheritedObj));
// after this call, psObj will have the correct vftable. And it's inherited
// methods will now be correctly callable.
psObj = deploy_vftable_cast<CInheritedObj *>(pvMem);
// will print 'inherited'
psObj->Blah();
}