Game Development Reference
In-Depth Information
const std::string& GetName() const
{
return m_name;
}
};
The destructor function also doesn't specify a return type and does not return a value. The
destructor is created by having a function that is named the same as the class itself but with a
~ prepended. All classes have destructor methods but again, the compiler will create a default
destructor if we do not. You do not have any code to put into your destructor at the moment;
however, we will look at how they can be used to greater effect in later chapters.
Note When we are talking about the code that makes up a class we are talking about classes. When we talk
about variables that are of a class type, we refer to them as objects. That means the class is the type, and an
object is an instance of the class.
This section has shown you how to specify the methods that are called when objects are created
and destroyed. The rest of this chapter covers some more advanced features that will be useful as
we move through the rest of the topic.
Method Overloading
C++ allows you to overload methods. This actually means that you can create methods of the same
name but that take different input parameters. You can see an example of this in Listing 8-9.
Listing 8-9. Overloading Player::SetName
class Player
{
private:
std::string m_name;
public:
Player()
{
}
Player(const std::string& name)
: m_name(name)
{
}
~Player()
{
}
 
Search WWH ::




Custom Search