I try to use the "new" operator to instanciate a specific class and not the one behind the "new" keyword.
I try to have a kind of "factory" for an abstract class.
It seems to me that it is not possible, but lets double check ! This code compile, but the main code treat it as a "Test" (and not a TestImpl" class)
class Test
{
public:
virtual int testCall() { return 0; };
static void* operator new(std::size_t);
};
class TestImpl : public Test
{
virtual int testCall() override
{
return i;
}
int i = 15;
};
void* Test::operator new(size_t sz)
{
return ::new TestImpl();
}
void main()
{
Test * t = new Test(); // Call the new operator, correctly
int i = test->testCall(); // i == 0 and not 15
}
Aucun commentaire:
Enregistrer un commentaire