C ++ dynamic_cast基类指向派生类指针的指针

时间:2015-11-05 21:38:33

标签: c++ pointers inheritance segmentation-fault dynamic-cast

我正在尝试制作一种非常简单的编程语言,让某人可以在Flex / Bison的学校项目中玩战舰游戏。为了存储我的变量,我有一个名为symbol_table的映射,它为键获取一个字符串,并为其值设置一个Variable *。继承树如下:

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){}
};

当我将Player指针和计算机指针放入地图时,一切都很有效,但当我尝试使用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值(否则程序将永远不会进入函数并立即给出分段错误)。

1 个答案:

答案 0 :(得分:0)

addBoat函数不是虚函数,因此在调用它之前无需取消引用myPlayer或myComputer,因此在函数后期才会出现段错误。

所有成员函数都有一个隐式this参数。您可以将addBoat视为等同于自由函数bool addBoat(Player* this, std::string boatName, char scolumn, int srow, bool vertical)。在您在函数中实际取消引用它之前,this为NULL是没有什么特别的。

要解决您遇到的实际问题,请在调用函数之前检查myPlayer和myComputer是否为NULL。