我最近收到了一个很好的答案,关于如何通过派生类来重载特定的类成员。
现在的问题是其中一个成员实际上是模板化的,其专业化要么是BaseClass
,DerivedClass
要么Derived2Class
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
template<class T>
class Queue
{
public:
Queue();
Queue(T*);
}
class Com
{
public:
virtual void setReady()
{
cout << "Com" << endl;
}
};
class DerivedCom : public Com
{
public:
void setReady()
{
cout << "DCom" << endl;
}
};
class Derived2Com : public Com
{
public:
void setReady()
{
cout << "D2Com" << endl;
}
};
class BaseClass
{
protected:
Com* com;
Queue<BaseClass>* queue;
public:
BaseClass(Com* c = new Com, Queue<BaseClass>* q = new Queue<BaseClass>) : com(c), queue(q)
{
}
void setReady()
{
com->setReady();
}
};
class DerivedClass : public BaseClass
{
public:
DerivedClass() : BaseClass(new DerivedCom, new Queue<DerivedClass>)
{
}
};
class Derived2Class : public BaseClass
{
public:
Derived2Class() : BaseClass(new Derived2Com, new Queue<Derived2Class>)
{}
};
int main()
{
BaseClass* base = new Derived2Class();
base->setReady();
return 0;
}
我可以毫无问题地“重载”简单的类,Com
但是构造函数签名不适合派生类试图发送它的类型。DerivedCom
Derived2Com
BaseClass