Skip to main content
gamedev.net
🔒 Locked

C++ object member variables.

Started by cs_vipa Jul 23, 2003 at 3:19 PM 2 replies 250+ views
Original Post
cs_vipa
cs_vipa
Hi, Im making an othello game project and ive got to the point where the basic game is done, but i need to add an intro and some way of deciding who you play against. My current structure is: There is a main class othelloProgram which is created in the main method. I want the othelloProgram to be in 3 main states, intro, game or newgame. othelloProgram .: has an int variable state and the method SetState is passed to the objects it creates. To make a game an othelloProgram has 2 objects of type player and 1 of type othelloGame, othelloGame''s constructor needs to be passed pointers to the players. othelloProgram also has an object of type intro. Initially I do not want the othelloGame or players to be created, their construction is dependant on the execution of the intro object, however the constructor forces me to make them which causes all kinds of errors. Also when intro eventually does run, i need to reconstruct the player and othelloGame objects. Is there any way to construct a class without constructing its member objects? Also how do you destroy and remake an object from a member method? Thanks for any help you can give, Vipa
sbennett
sbennett
quote:
Original post by cs_vipa
Is there any way to construct a class without constructing its member
objects? Also how do you destroy and remake an object from a member method?



Pointers. new and delete.

In your program class, store pointers to the subobjects. Initialise them to NULL in the constructor, then allocate them when you need them. Then, in the program class''s destructor, check to see whether they are NULL, and if not, delete them.
GrinningGator
GrinningGator
If I understand you correctly, you want to have a Game object hold player objects, but you can't construct the player objects until after the game object does some work. (is that right?)

Since you didn't say, I'm assuming this is in C++. Couldn't your game class store pointers to the player objects? When the game class is constructed you can initialize your player pointers to null, then allocate them using new when you want to construct them.

Edit: typed too slow

[edited by - GrinningGator on July 23, 2003 5:02:48 PM]
Lektrix
Lektrix
You can use pointers and then use the new and delete operators to control when objects are constructed (the lifetime). Remember to write copy and destruction functions for the class so that the dynamically allocated objects are freed. The most common way is to use null to represent a pointer that doesn''t point to allocated memory. On construction you would initialise the pointers to null (0/NULL). You can then dynamically allocate when you wish. As for destruction you would call delete on any pointers that are not null, i.e. the members that point to memory allocated using new.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.