W
WittyGuy
Hi
My class looks something like this:
class Base
{
public:
Base () {}
~Base () {} // No virtual dtor in the base class
private:
};
class Derived :
public Base
{
public:
Derived () {}
~Derived () {}
private:
};
int
main ()
{
Base *pB = new Derived; // how to *restrict* this type of
instantiation
delete pB;
Derived *pD = new Derived; // Should allow this type of
instantiation
delete pD;
return 0;
}
As given in the above sample code, how to restrict the instantiation
of derived class object in the heap when it's assigned to a Base class
pointer rather should allow the same if it's assigned to pointer of
same type. Coz the later form of instantiation can be allowed as there
is no harm of memleak though virtual dtor is not defined in base
class.
Basically this query araised coz, I'm inheriting from stl container.
Yes I know ideally I shouldn't have done this coz STL container's dtor
are not virtual dtor. But anyways if there is a way of restricting the
instantiation of object as explained above, it should solve my
problem.
-Wg-
My class looks something like this:
class Base
{
public:
Base () {}
~Base () {} // No virtual dtor in the base class
private:
};
class Derived :
public Base
{
public:
Derived () {}
~Derived () {}
private:
};
int
main ()
{
Base *pB = new Derived; // how to *restrict* this type of
instantiation
delete pB;
Derived *pD = new Derived; // Should allow this type of
instantiation
delete pD;
return 0;
}
As given in the above sample code, how to restrict the instantiation
of derived class object in the heap when it's assigned to a Base class
pointer rather should allow the same if it's assigned to pointer of
same type. Coz the later form of instantiation can be allowed as there
is no harm of memleak though virtual dtor is not defined in base
class.
Basically this query araised coz, I'm inheriting from stl container.
Yes I know ideally I shouldn't have done this coz STL container's dtor
are not virtual dtor. But anyways if there is a way of restricting the
instantiation of object as explained above, it should solve my
problem.
-Wg-