我正在尝试编写一个非常简单的编程语言,让某人在Flex/Bison中为某个学校项目玩一个战舰游戏。为了存储我的变量,我有一个名为symbol_table的映射,它为该键提供一个字符串,并为其值赋予一个变量*。继承树如下:C++ dynamic_cast基类指向派生类指针的指针
class Expression {
public:
Type type;
virtual ~Expression(){} //to make it polymorphic
};
class Variable : virtual public Expression {
public:
char* name;
Variable* next;
virtual ~Variable(){ //also for polymorphism
delete name;
delete next;
}
};
class Player : virtual public Variable{
public:
std::string playerName;
std::vector<GameBoat> playerBoats;
Grid opponentGrid;
Grid playerGrid;
Player(std::string playerName){
this->playerName = playerName;
}
bool addBoat(std::string boatName, char scolumn, int srow, bool vertical){
//make new boat here and then push it back to the vector
GameBoat newBoat(boatName); //this works --> makes a valid boat
if(newBoat.assignedValue){
playerBoats.push_back(newBoat); //SEGMENTATION FAULT HAPPENS HERE
return true;
}else{
return false;
}
}
};
class Computer : public Player{
public:
Computer(std::string playerName) : Player(playerName){}
};
一切都很正常,当我把球员指针和计算机指针到地图,但是当我尝试检索这些值再次使用的dynamic_cast的基础变量*垂头丧气的球员*或计算机*,所播放的播放器*或计算机*的所有属性均为NULL,因此会给我一个“分段错误:11”错误。但是,我可以访问Player和Computer类中的类方法。
Variable* var = get_symbol($1);
if (var == NULL) {
sprintf(errormsg, "%s not found.\n", $1);
yyerror(errormsg);
}else if(var->type == PlayerType){
Player* myPlayer = dynamic_cast<Player*>(var); //Cast var to Player*
myPlayer->addBoat("aircraftcarrier", 'a', 1, true); //Enters this function
}else if(var->type == ComputerType){
Computer* myComputer = dynamic_cast<Computer*>(var); //Cast var to Computer*
myComputer->addBoat("aircraftcarrier", 'a', 1, true); //Enters this function
}
我该如何访问派生类的方法,但无法访问派生类的属性?我使用多态,dynamic_cast不返回NULL值(否则程序将永远不会进入函数并立即给出分段错误)。
您正在创建一种编程语言,仅用于创建战舰游戏? –
这是一个学校项目。这是重点。 – tdon
您能否提供[最小,完整和可验证的示例](http://www.stackoverflow.com/help/mcve)?我不知道问题是什么。 – Barry