У меня небольшая проблема с моим производным классом. В основном у меня есть суперкласс Object
и производный класс UnmovableObject
. Я пытаюсь добавить булеву переменную в производный класс, чтобы позже я смог прочитать ее и посмотреть, могут ли мои объекты быть перемещены или нет. У меня проблема в том, что я храню все объекты (супер и производные) в list<Object> inventory
. Каждый раз, когда я читаю значения из списка, я получаю странное значение (204) для метода isFixed()
. Это код:
//super class
#pragma once
#include "stdafx.h"
class Object{
public:
Object(); //constructor
Object(const string name, const string description); //constructor
~Object(); //destructor
private:
string nameOfObject; //the name of the room
string objectDescription; //the description of the room
};
//derived class
#pragma once
#include "stdafx.h"
#include "object.h"
//This class creates unmovable objects - the user can't pick them up.
class UnmovableObject : public Object {
public:
UnmovableObject(string name, string description);
UnmovableObject(const Object &object) : Object(object){};
bool isFixed();
private:
bool fixed;
};
//the constructor of this class takes a boolean value (by default true) - the object is fixed in this room
UnmovableObject::UnmovableObject(string name, string description) : Object(name, description){
this->fixed = true;
}
//returns false as the object is not movable
bool UnmovableObject::isFixed(){
return this->fixed;
}
//other class
list<Object> inventory;
Как я могу использовать inventory.push_back(Object/UnmovableObject);
, чтобы при попытке доступа к inventory
я мог получить правильное логическое значение для всех из них & mdash; true
для UnmovableObject; false
для объекта.