是否可以在基类中有一个函数,其返回类型为指向派生类的指针?主要目标是供以后在您使用Base
和Derived
Class 设置值的地方使用。
Base B1;
B1.SetName("nameOfBase");
Derived* D1 = B1.CreateDerived("DerivedFromBase");//CreateDerived method will be in class Base
D1->SetMinPoint(0,1);//this method will be implemented in derived class
D1->SetMaxPoint(4,4);//this method will be implemented in derived class
我在实施中遇到问题,我做了类似的事情
class Base
{
public:
Base();
bool SetName(char*);//or SetName(string)
Derived* CreateDerived(char*); // or Derived* CreateDerived(string)
~Base();
protected:
char baseName[20];// or string baseName
Derived* derivedPtr[5];
};
class Derived: public Base
{
public:
Derived();
bool SetName(char*);//the name given in Derived* CreateDerived(char*) will be set here
~Derived();
};
当我尝试执行此操作并运行程序时,我会收到类似的错误
// Derived* CreateDerived(char*); // or Derived* CreateDerived(string)
error C2143: syntax error: missing ';' before '*'
error C4430: missing type identifier: int assumed.