【发布时间】:2011-10-19 03:16:13
【问题描述】:
抱歉,源代码量较大。存在三个抽象类P、L、PL。第三个类 PL 是使用虚拟继承从类 P 和 L 派生的:
template <typename T> //Abstract class
class P
{
public:
virtual ~P() = 0;
virtual P <T> *clone() const = 0;
};
template <typename T>
P<T>::~P() {}
template <typename T>
P<T> * P <T>:: clone() const { return new P <T> ( *this ); }
template <typename T> //Abstract class
class L
{
public:
virtual ~L() = 0;
virtual L <T> *clone() const = 0;
};
template <typename T>
L<T>::~L() {}
template <typename T>
L<T> *L <T> ::clone() const { return new L <T> ( *this );}
template <typename T>
class PL: virtual public P <T>, virtual public L <T> //Abstract class
{
public:
PL() : P <T>(), L<T>() {}
virtual ~PL() = 0;
virtual PL <T> *clone() const = 0;
};
template <typename T>
PL<T>::~PL() {}
template <typename T>
PL<T> * PL <T> :: clone() const { return new PL <T> ( *this );}
每个类都有自己的克隆方法实现。
接下来的两个类 PA、PC 是使用虚拟继承从类 P 派生的:
template <typename T>
class PC : virtual public P <T>
{
public:
PC() : P <T> () {}
virtual ~PC() {}
virtual PC <T> *clone() const {return new PC <T> ( *this );}
};
template <typename T>
class PA : virtual public P <T>
{
public:
PA() : P <T> () {}
virtual ~PA() {}
virtual PA <T> *clone() const {return new PA <T> ( *this );}
};
最后两个类 PCL 和 PAL 是使用从 PC 和 PL 、 PA 和 PL 的虚拟继承来驱动的。
template <typename T>
class PCL : public PC <T>, public PL <T>
{
public:
PCL() : P <T> (), PC <T> (), PL <T> () {}
virtual ~PCL() {}
virtual PCL <T> *clone() const {return new PCL <T> ( *this );}
};
template <typename T>
class PAL : public PA <T>, public PL <T>
{
public:
PAL() : P <T> (), PA <T> (), PL <T> () {}
virtual ~PAL() {}
virtual PAL <T> *clone() const {return new PAL <T> ( *this );}
};
有类依赖关系图:
.......... P .... L.....
........../|\..../......
........./.|.\../.......
......../..|..\/........
.......PC..PA..PL.......
.......|...|.../|.......
.......|...|../.|.......
.......|...PAL..|.......
.......|........|.......
.......PCL_____/........
请不要讨论这个提议:-)))。我有以下 3 个问题:
1) 这个类依赖是否用 C++ 正确重写(首先是“虚拟”的位置)?
2) 不知道哪里出错了,请看代码:
int main(int argc, _TCHAR* argv[])
{
PCL <double> * pcl = new PCL <double>(); //Object of abstract class not allowed
PAL <double> * pal = new PAL <double>(); //Object of abstract class not allowed
PL <double> *pl1 = pcl->clone(); //Polymorphism
PL <double> *pl2 = pal->clone(); //Polymorphism
return 0;
}
无法创建 PAL / PCL 类的新对象,这两个类都标记为抽象类。但它们并不抽象。问题出在哪里?
3) 是否可以将多态性与 clone() 方法一起使用?请看上面的代码...
感谢您的帮助...
更新问题
我更正了代码。但是使用VS 2010编译器出现如下错误:
template <typename T> //Abstract class
class P
{
public:
virtual ~P() = 0;
virtual P <T> *clone() const = 0;
};
template <typename T>
P<T>::~P() {}
template <typename T>
P<T> * P <T>:: clone() const { return new P <T> ( *this ); }
template <typename T> //Abstract class
class L
{
public:
virtual ~L() = 0;
virtual L <T> *clone() const = 0;
};
template <typename T>
L<T>::~L() {}
template <typename T>
L<T> *L <T> ::clone() const { return new L <T> ( *this );}
template <typename T>
class PL: virtual public P <T>, virtual public L <T> //Abstract class
{
public:
PL() : P <T>(), L<T>() {}
virtual ~PL() = 0;
virtual PL <T> *clone() const = 0;
};
template <typename T>
PL<T>::~PL() {}
template <typename T>
PL<T> * PL <T> :: clone() const { return new PL <T> ( *this );}
template <typename T>
class PC : virtual public P <T>
{
protected:
T pc;
public:
PC() : P <T> () {}
virtual ~PC() {}
virtual PC <T> *clone() const {return new PC <T> ( *this );}
};
template <typename T>
class PA : virtual public P <T>
{
public:
PA() : P <T> () {}
virtual ~PA() {}
virtual PA <T> *clone() const {return new PA <T> ( *this );}
};
template <typename T>
class PCL : public PC <T>, public PL <T>
{
public:
PCL() : P <T> (), PC <T> (), PL <T> () {}
virtual ~PCL() {}
virtual PCL <T> *clone() const {return new PCL <T> ( *this );}
}; //Error using VS 2010: Error 1 error C2250: 'PCL<T>' : ambiguous inheritance of 'PC<T> *P<T>::clone(void) const'
template <typename T>
class PAL : public PA <T>, public PL <T>
{
public:
PAL() : P <T> (), PA <T> (), PL <T> () {}
virtual ~PAL() {}
virtual PAL <T> *clone() const {return new PAL <T> ( *this );}
}; //Error VS 2010: Error 1 error C2250: 'PAL<T>' : ambiguous inheritance of 'PA<T> *P<T>::clone(void) const'
int main(int argc, char* argv[])
{
PCL <double> * pcl = new PCL <double>();
PAL <double> * pal = new PAL <double>();
PL <double> *pl1 = pcl->clone();
PL <double> *pl2 = pal->clone();
return 0;
}
也许我忽略了一些东西...但是 g++ 编译此代码正常。
【问题讨论】:
-
P、L和PL中的纯虚拟析构函数有默认实现吗? -
我认为你不能在纯函数声明中添加定义;说
= 0;。在PL <T> { }之后也缺少括号(应该是PL <T>() { })。正如@Chad 所说,如果您已经拥有其他纯虚函数,则无需将析构函数设为纯虚函数。 -
@Chad:是的,我更正了代码...
标签: c++ inheritance virtual abstract-class clone