Game Development Reference
In-Depth Information
Chapter 12
Copying and Assigning Data
to Objects
You have seen how we can pass data into functions by value, by reference, or by pointer. If you
choose to pass a class object into a function by value, C++ will make a copy of the class. This copy
operation happens automatically or implicitly . This chapter is going to show you some of these
implicit operations that can occur when using classes in your programs and the ways in which you
can alter and manage these implicit operations.
Copy Constructors
Classes in C++ can use constructors and destructors to initialize and clean up the objects that
you use in your programs. C++ also allows you to specify a copy constructor that is called when
an object is being copied into a new instance of an object. Listing 12-1 shows a simple copy
constructor.
Listing 12-1. A Copy Constructor
class Player
: public Entity
{
private:
const Room* m_pCurrentRoom;
std::string m_name;
public:
Player()
{
}
139
 
Search WWH ::




Custom Search