Data items are private by default in the class. Thus, the variables cur_room , name are different in the Agent and Grue classes. When you call the Grue constructor, the Grue fields are initialized, but the vector retains the agent pointers, so you get access to the agent fields that are not initialized.
here is the correct way:
class Agent
{
protected:
Room *cur_room;
string name;
public:
Agent(string n, Room * r)
: cur_room (r), name(n)
{}
};
class Grue : public Agent
{
public:
Grue(string n, Room * r):Agent(n,r)
{
}
};
source
share