I'm learning the basics of classes and came across this one piece of code in the example. I've made my version of it and tried to remove that particular section and came up with an error. What exactly does that particular piece of code do?
Not really, these lines define the constructor declared in the class (player (int, int, int);).
That version of the constructor initializes the member variables with the passed parameters.
player Default(); That defines a function that returns a player an receives nothing player Default; That creates a player using the default constructor.
1 2 3 4
while (player.check() > 10)
{
player.speed--;
}
player is a class. The methods are executed in objects.
And it seems that you are trying to access a private member. You can't do that outside of methods of the class (or friends).
You don't need the () if you want a default constructor to be used. In fact, if you do use the () with no arguments, then C++ will assume you wanted to define a function. I think. I need coffee. :/
Also... I don't see an object named "player" anywhere, so why are you trying to access its member on lines 38 and 40? >_>
So how would you be able to access the objects within the class dynamically? Say if the game has hundreds of players to choose from and I want to run a conditional statement that checks each and every player's stats (as well as manipulating them).
Okay. I think there's some sort of misunderstanding as far as terminology goes.
A class is like a blueprint. The compiler has to know what it is when you're compiling. You cannot change this blueprint at run-time (dynamically). Although, with some clever and rather dangerous tricks, you can make it seem like it does this. Please don't do it, though, unless you really know what you're doing.
Also, LBEaston's right. That's one of the safest and best ways to do it, although if you have hundreds and they consume a lot of memory, then I might suggest a deque. Right now, they don't really. :)