Where do I place srand()? And other help needed

Started by
11 comments, last by Shakedown 16 years, 11 months ago
Sorry, I wasn't very explicit - my suggested change will fix your random-name problem, yes. Constructors are executed when an object is created, so as long as you don't create the object until after your srand() call, the rand() call in the constructor will work as expected.

When you create an object inside of a function (as opposed to a global object), it's created basically in chronological order. That is, for this code:

a();SomeClass foo;OtherClass bar;StillOtherClass quux;b();


The function a() will be called, then the objects will be constructed in the order foo, bar, quux, and then b() will be called.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Advertisement
Short answer is yes the pointer would still be global to everything in file.

CCharacter myCharacter //created on the stack upon execution...at startupCCharacter * myCharacter = 0 // pointer is created on the stack upon execution


Pointers 'point' to other objects. They contain a hexidecimal value "0xAABBCCDD" which is the RAM address of the object they are pointing to. The declaration and inialization of the pointer above is basically right now 'pointing' at nothing(which is okay we don't need it yet ).

When you would like to 'create' your player in any function at any time, becasue the pointer is global, you use the keyword "new". "New" basically tells the compiler: "Hey I want to create this object now"

myCharacter = new CCharacter( hp, ap, def ) 


If you notice...the constructor for CCharacter is being called and internally what it is going is the processor is creating the CCharacter object in a special place in RAM called the 'heap'. Nothing special about this place really its just where our pointer data goes.

So the pointer itself is global created on the stack and the data it 'points' to is on the 'heap'. You can access pointer data at anytime by using the dereference operator "*" in front of the pointer.

int * myIntPointer; // the pointer is created here, but not the intmyIntPointer = new int(5); // creates the integer here of value 5cout<< *myIntPointer; //dereferences the pointer to get our integer on the heap*myIntPointer = 6; // changes the data of the pointer from 5 to 6.delete myIntPointer; //remove the pointer from the heap.


The thing to know about pointers is that they are very stupid. You bacially have to tell the pointer when to create and when to destroy and if you don't destroy the pointer, that data will sit in memory forever. So always make sure to delete pointers after you've created them(ie: if you use "new"...then use "delete".

In your case you would want to use a pointer to create your player object.

CCharacter * myPlayer = 0; //player pointer...it points to nothing right now.//Some time later in another functionmyPlayer = new CCharacter( hp,  ap, def ) //creates the data for CCharacter//Even more time later...show player data to the screen(*myPlayer).displayStats(); // calls displayStats inside of the CCharacter class//However, C++ offers a better implementation than the way used abovemyPlayer->displayStats(); //better and more simpler version...use this one


This demonstates the basic use of pointers. Use the arrow operator when dealing with object pointers and use the dereference operator '*' when using atomic types like 'int' and 'double'.

Anymore question about pointers....feel free to ask.

EDIT:: the above posts offer a simpler solution than using pointers. If you don't feel like learning pointers then feel free to ignore this post.
Beautiful. I'm liking these replies. I'll be able to implement these suggestions here shortly, hopefully it works out. Thank you all for your help!

This topic is closed to new replies.

Advertisement