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 .h
file. That is, start the file with:
#ifndef PLAYER_H
#define PLAYER_H
// file contents go here
#endif // PLAYER_H
Make sure you have all required #include
s
The code uses std:string
within player.h
but doesn't #include <string>
. Also, carefully consider which #include
s 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 <iostream>
#include <string>
#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.