I have a private three dimensional vector of shared_ptr<Room>
objects as follows:
private:
vector<vector<vector<shared_ptr<Room>>>> world;
In the same class, I provide access to Room
objects:
public:
shared_ptr<Room> room_at(const int & x, const int & y, const int & z) const
{
return world.at(x).at(y).at(z);
}
Also in the same class, I initialize the world
structure:
for (int x = 0; x < C::WORLD_X_DIMENSION; ++x)
{
vector<vector<shared_ptr<Room>>> row;
for (int y = 0; y < C::WORLD_Y_DIMENSION; ++y)
{
vector<shared_ptr<Room>> vertical_stack; // "stack"
for (int z = 0; z < C::WORLD_Z_DIMENSION; ++z)
{
vertical_stack.push_back(shared_ptr<Room>(nullptr));
}
row.push_back(vertical_stack);
}
world.push_back(row);
}
Later, I want to save a Room
object into world
:
void add_room_to_world(const int & x, const int & y, const int & z)
{
shared_ptr<Room> room = make_shared<Room>(); // create an empty room
/* (populate room's member fields) */
// add room to world
room_at(x, y, z) = room; // This doesn't work as expected
}
The shared_ptr
within world
starts out as nullptr
as expected, but doesn't change on the last line above.
Based on what I've found on SO, I've tried operator=
(above), .reset(room)
, and make_shared<Room>(room)
(using an actual Room
object rather than shared_ptr<Room>
) but in all cases, the shared_ptr
within world
stays set to nullptr
.
What is the correct way to assign objects into world
?
Aucun commentaire:
Enregistrer un commentaire