Variable Holding Class Instance [C++]

Started by
4 comments, last by timmeh_1250 15 years, 2 months ago
I have been learning/studying basic programming logic for a few years, and have recently decided to pick up C++ as my first professional programming language. I've run through the usual "Hello Word" and Guessing Game tutorials, and am beginning to get a grasp on the context and basics of C++. As such, I've decided to start a (very) basic interactive fiction project. All I want (to begin with, at least) is rooms I can walk between. As it is, I've set up a class Room, that has a name and description, as well as a function that outputs the name and description I pass to it, in the format I need. Now I need a variable to hold both the room the player is currently standing in, as well as the rooms in each direction, for each instance of the Room class. However, I've run into a bit of trouble, in that I don't know what type of variable to use to hold instances of a class. In short, my question is, what variable type would I use to store instances of a class? If I've failed to express the problem clearly enough, please let me know what I've written with too little clarity so I can expand on it asap. My thanks to any and all who help with this issue, Timothy Sassone
Advertisement
So you've defined your class:


class Room {
string name;
string description;
}


Now you've essentially just defined a new variable type - called Room - that you can use to store instances of the class, i.e.,

Room room1;
Room room2;


Quote:Now I need a variable to hold both the room the player is currently standing in, as well as the rooms in each direction, for each instance of the Room class. However, I've run into a bit of trouble, in that I don't know what type of variable to use to hold instances of a class.


By variable, you mean data structure. You've got your variable - Room - but you need a data structure to hold all your Room's.
For instance you can use a RoomManagerClass, which manages the creation and release of different room instances:

RoomManager.h:#include <vector>class Room;class RoomManager{public:  RoomManager();  ~RoomManager();  Room *create();    void destroy( Room *pRoom );  Room *getRoomByName( const std::string &rRoomName );private:  std::vector<Room*> m_Rooms;};RoomManager.cpp:RoomManager::RoomManager(){}RoomManager::~RoomManager(){  for ( std::vector<Room*>::iterator it = m_Rooms.begin(); it != m_Rooms.end(); ++it)  {    delete *it;  }}Room *RoomManager::create()  {  Room *pRoom = new Room;  Rooms.push_back( pRoom );  return pRoom;}...


This is just a simple example to show how a ( very ) simple room-manager can work.

With best regards,
Kimmi
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
My thanks to both of you. Unfortunately, I think I've mis-worded my problem. I don't so much a need a data structure, so much as I need to know what data type can hold the rooms.

Basically, I want to be able to assign a room to a variable, so a variable can be used to track the current room, and additional class variables in each room can store the names of the rooms adjacent to it.

Here's what I have so far:
class Room {      public:              string name;              string desc;};


I want to store the room in a variable, so that if I were to create a room ("Room home;", for example) I could store it in a variable like so:
vartype current_room = home

or at least store the address of the rooms as pointers...
vartype current_room = &home


But I don't know which data type should be used to store such information.


If I missed something, or if one of your answers was what I need and I just missed it please let me know. I'm not experienced enough to really pick up on the code snippets just yet, and I'm not 100% sure about some of the context in the code Kimmi graciously provided.

My apologies if this is an incredibly basic or stupid question, but I'm not really sure how to figure this one out myself...

Thanks again,
Timothy Sassone
Quote:Original post by timmeh_1250
Basically, I want to be able to assign a room to a variable, so a variable can be used to track the current room, and additional class variables in each room can store the names of the rooms adjacent to it.

I want to store the room in a variable, so that if I were to create a room ("Room home;", for example) I could store it in a variable [...] or at least store the address of the rooms as pointers...


If you're happy storing the address of the rooms as pointers, why not use a pointer to room?

Room* current_room = &home
Quote:Original post by Geoff the Medio
Quote:Original post by timmeh_1250
Basically, I want to be able to assign a room to a variable, so a variable can be used to track the current room, and additional class variables in each room can store the names of the rooms adjacent to it.

I want to store the room in a variable, so that if I were to create a room ("Room home;", for example) I could store it in a variable [...] or at least store the address of the rooms as pointers...


If you're happy storing the address of the rooms as pointers, why not use a pointer to room?

*** Source Snippet Removed ***


Sometimes I feel incredibly stupid.... I can't believe I missed a solution that simple... oh well, guess that's part of the learning process.

Thanks for your help everyone!

Tim

This topic is closed to new replies.

Advertisement