patterncppModerate
Is there a good way to do this C++ single-player class?
Viewed 0 times
thisplayerwaysinglegoodthereclass
Problem
I am making a little single-player game and to help with scoring I have created a
I know that for a multiplayer game this isn't good. For multiplayer I would need to call functions and have all players in one instance.
File player.h
File player.cpp
File main.cpp
The code works as expected. Thank you for any feedback and assistance in improving my single player class.
Player class. Can you review my implementation?I know that for a multiplayer game this isn't good. For multiplayer I would need to call functions and have all players in one instance.
File player.h
namespace SDLGamecore {
namespace player {
class Player
{
private:
long cash;
int workers;
std::string name;
public:
Player(std::string playerName)
{
name = playerName;
cash = 0;
workers = 0;
}
~Player(){}
long getCurrentCash() const;
int getWorkers() const;
std::string getPlayerName() const;
};
}}File player.cpp
namespace SDLGamecore {
namespace player {
long Player::getCurrentCash() const
{
return cash;
}
int Player::getWorkers() const
{
return workers;
}
std::string Player::getPlayerName() const
{
return name;
}
}}File main.cpp
Player player("Test");
std::string pName = player.getPlayerName();
std::cout << pName << std::endl;The code works as expected. Thank you for any feedback and assistance in improving my single player class.
Solution
There's not a lot of substance here to review, but anyway, here are some things that may help you improve your code.
Use include guards
There should be an include guard in the
Make sure you have all required
The code uses
Let the compiler create default destructor
The compiler will create a destructor by default which is essentially identical to what you've got, so you can simply omit both the declaraton and implementation from your code.
Prefer modern initializers for constructors
The constructor could use the more modern initializer style rather than the old style you're currently using. Instead of this:
You could use this:
Write member initializers in declaration order
When you start writing constructors in the modern style mentioned above, you'll want to make sure that you write the initializers in the same order as they are declared. Because members are always initialized in declaration order and
Don't write Java in C++
While it may be fashionable in Java to write canonical getters and setters of the form you've got, it's definitely not C++ style. Consider these two lines from
We already know the object is a
Rethink namespaces
Is it really necessary to have a
Consider using inheritance
It's likely that your eventual game is going to have more than just
Provide the entire code to simplify reviews
Finally, it's not really about the code but about the review process. A minimal real
It's usually better to post code that will actually compile without modification rather than snippets of code that require the reviewer to imagine the missing bits. Including everything needed will help you get better and more useful reviews.
Use include guards
There should be an include guard in the
.h file. That is, start the file with:#ifndef PLAYER_H
#define PLAYER_H
// file contents go here
#endif // PLAYER_HMake sure you have all required
#includesThe code uses
std:string within player.h but doesn't #include . Also, carefully consider which #includes are part of the interface (and belong in the .h file) and which are part of the implementation.Let the compiler create default destructor
The compiler will create a destructor by default which is essentially identical to what you've got, so you can simply omit both the declaraton and implementation from your code.
Prefer modern initializers for constructors
The constructor could use the more modern initializer style rather than the old style you're currently using. Instead of this:
Player(std::string playerName)
{
name = playerName;
cash = 0;
workers = 0;
}You could use this:
Player(std::string playerName) :
cash{0},
workers{0},
name{playerName}
{ }Write member initializers in declaration order
When you start writing constructors in the modern style mentioned above, you'll want to make sure that you write the initializers in the same order as they are declared. Because members are always initialized in declaration order and
cash is declared before name in this class. To avoid misleading another programmer, you should make the order match in all constructors.Don't write Java in C++
While it may be fashionable in Java to write canonical getters and setters of the form you've got, it's definitely not C++ style. Consider these two lines from
main:std::string pName = player.getPlayerName();
std::cout << pName << std::endl;We already know the object is a
player, so saying player.getPlayerName() seems redundant. I'd prefer to just write player.name() like this:std::cout << player.name() << std::endl;Rethink namespaces
Is it really necessary to have a
player namespace encapsulating a Player class? Specifying your own namespace is a good idea, but nesting them deeply is probably not. Consider using inheritance
It's likely that your eventual game is going to have more than just
Player class objects and it's also likely that those other objects will also have names. This suggests that it might make sense to have a base class that includes common things such as name and then derive more specialized objects from it.Provide the entire code to simplify reviews
Finally, it's not really about the code but about the review process. A minimal real
main.cpp would look something like this:#include
#include
#include "player.h"
int main() {
using namespace SDLGamecore::player;
Player player("Test");
std::cout << player.name() << std::endl;
}It's usually better to post code that will actually compile without modification rather than snippets of code that require the reviewer to imagine the missing bits. Including everything needed will help you get better and more useful reviews.
Code Snippets
#ifndef PLAYER_H
#define PLAYER_H
// file contents go here
#endif // PLAYER_HPlayer(std::string playerName)
{
name = playerName;
cash = 0;
workers = 0;
}Player(std::string playerName) :
cash{0},
workers{0},
name{playerName}
{ }std::string pName = player.getPlayerName();
std::cout << pName << std::endl;std::cout << player.name() << std::endl;Context
StackExchange Code Review Q#133924, answer score: 11
Revisions (0)
No revisions yet.