class MeshGeneration{
public:
static MeshGeneration CreateUnstrMesh() {
cout<<"Unstr called"<<endl;
return MeshGeneration(0);}
static MeshGeneration CreateStrMesh() {
cout<<"Str called!"<<endl;
return MeshGeneration(1);}
virtual void CreateHybridMesh(){}
protected:
MeshGeneration(int mesh_type = -1){
string mstring;
if(mesh_type == 0)
mstring = "unstructured";
else if(mesh_type == 1)
mstring = "structured";
else;
cout <<"mesh_type = "<<mstring<<endl;
}
};
class DerivedMeshGeneration:public MeshGeneration{
public:
void CreateHybridMesh(){
cout<<"mesh_type = hybrid"<<endl;
}
};
int main(int argc, char * argcv[]){
MeshGeneration m1 = MeshGeneration::CreateUnstrMesh();
MeshGeneration m2 = MeshGeneration::CreateStrMesh();
MeshGeneration m3 = DerivedMeshGeneration::CreateUnstrMesh();
m3.CreateHybridMesh(); // not working as expected..
return 0;
}
最后的功能无法正常工作。我认为 当我继承基类时有些事情是错误的。任何建议表示赞赏! 吧。继承基类的构造函数命名问题
谢谢。我尝试了“DerivedMeshGeneration m3 = DerivedMeshGeneration :: CreateUnstrMesh();”,当我编译时,我得到了错误消息,如“错误:从'MeshGeneration'转换为非标量类型'DerivedMeshGeneration'请求”,对此的任何建议? – stonebird 2011-02-16 20:49:02
@ user606769:DerivedMeshGeneration :: CreateUnstrMesh()的返回类型仍然是`MeshGeneration`,因为它是基类中的函数。而且,您不能将基类类型的对象分配给派生类类型的对象。 – Xeo 2011-02-16 22:29:45