F
feel
Hi,All
I am sure it's an old question. But I just find a interesting design
about this: Polymorphism without virtual function in a C++ class.
My solution is for some special case, trust me, very special.
1 single root class tree
2 the leaf(lowest level) classes are sealed which means we should not
inherite class from them.
3 PImpl idiom. There is only one data mumber in root class and there
is no any other data mumber in child class and virtual funtions.
In my solution, the destructor of root class is not virtual, but we
can use base class pointer to point derived class object.
My question is: is this design follow the C++ standard? I tested it in
VS2005. it's ok. How about GCC?I remember that this non-virtual
destructor behavor is undefine in C++ standard.
Here is a simple code example:
class base
{
public:
~base()
{
delete[] p;
};
protected:
int *p;
base()(new int[10])
{
};
base(int *pp) : p(pp)
{
};
};
class base1 : public base
{
protected:
base1()
{
};
};
class my : public base1
{
public:
my ()
{
p = new int[10];
};
};
int _tmain(int argc, _TCHAR* argv[])
{
base1 *o = new my;
delete o;
return 0;
}
I am sure it's an old question. But I just find a interesting design
about this: Polymorphism without virtual function in a C++ class.
My solution is for some special case, trust me, very special.
1 single root class tree
2 the leaf(lowest level) classes are sealed which means we should not
inherite class from them.
3 PImpl idiom. There is only one data mumber in root class and there
is no any other data mumber in child class and virtual funtions.
In my solution, the destructor of root class is not virtual, but we
can use base class pointer to point derived class object.
My question is: is this design follow the C++ standard? I tested it in
VS2005. it's ok. How about GCC?I remember that this non-virtual
destructor behavor is undefine in C++ standard.
Here is a simple code example:
class base
{
public:
~base()
{
delete[] p;
};
protected:
int *p;
base()(new int[10])
{
};
base(int *pp) : p(pp)
{
};
};
class base1 : public base
{
protected:
base1()
{
};
};
class my : public base1
{
public:
my ()
{
p = new int[10];
};
};
int _tmain(int argc, _TCHAR* argv[])
{
base1 *o = new my;
delete o;
return 0;
}