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