I want to create a game that runs in the command prompt. Not sure if it'll be only text based or will have basic ASCII art yet. But anyways, I'm setting up the class that will store player data.
My question is:
I have a lot of member variables in my class. Is there a better way to type my overload constructor than what I have now? Otherwise each time I add a new variable, it's going to make a very very long list of arguments.
If you are unfamiliar with move semantics, ignore the std::move for now; write it as
1 2
Player( std::string id, std::string name ) : UUID(id), username(name))
{ /* validate id, name. etc. */ }
> what do you mean "validate id name. etc."
The class may have (often would have) invariants: for example, that the name or id can't be empty strings.
It is the responsibility of the constructor to establish the class invariant;
do something to ensure that the object is not initialised to an invalid state.
For example:
1 2 3 4 5 6 7
Player( std::string id, std::string name ) : UUID(id), username(name)
{
if( name.empty() ) name = "unknown" ; // silently correct the name
// (the alternative would be to throw an exception)
// etc.
}