[ fromfile: copyassign.xml id: copyassign ]
C++ gives almost god-like powers to the designer of a class. Object "life cycle" management means taking complete control over the behavior of objects during birth, reproduction, and death. You have already seen how constructors manage the birth of an object and how destructors are used to manage the death of an object. This section investigates the reproduction process: the use of copy constructors and assignment operators.
A copy constructor is a constructor that has a prototype like this:
ClassName(const ClassName & x);
The purpose of a copy constructor is to create an object that is an exact copy of an existing object of the same class.
An assignment operator for a class overloads the symbol =
and gives it a meaning that is specific to the class.
There is one particular version of the assignment operator that has the following prototype:
ClassName& operator=(const ClassName& x);
Because it is possible to have several different overloaded versions of the operator=()
in a class, we call this particular version the copy assignment operator.
Example 2.16. src/lifecycle/copyassign/fraction.h
[ . . . . ] class Fraction { public: Fraction(int n, int d) ; Fraction(const Fraction& other) ; Fraction& operator=(const Fraction& other) ; Fraction multiply(Fraction f2) ; static QString report() ; private: int m_Numer, m_Denom; static int s_assigns; static int s_copies; static int s_ctors; }; [ . . . . ]
<include src="src/lifecycle/copyassign/fraction.h" href="src/lifecycle/copyassign/fraction.h" id="cafractionh" mode="cpp"/>
The version of Fraction
in Example 2.16 has three static
counters, defined in Example 2.17, so that you can count the total number of times each member function is called.
This should help you to better understand when objects are copied.
Example 2.17. src/lifecycle/copyassign/fraction.cpp
[ . . . . ] int Fraction::s_assigns = 0; int Fraction::s_copies = 0; int Fraction::s_ctors = 0; Fraction::Fraction(const Fraction& other) : m_Numer(other.m_Numer), m_Denom(other.m_Denom) { ++s_copies; } Fraction& Fraction::operator=(const Fraction& other) { if (this != &other) { m_Numer = other.m_Numer; m_Denom = other.m_Denom; ++s_assigns; } return *this; } [ . . . . ]
<include src="src/lifecycle/copyassign/fraction.cpp" href="src/lifecycle/copyassign/fraction.cpp" id="cafractioncpp" mode="cpp"/>
Example 2.18 uses this class to create, copy, and assign some objects.
Example 2.18. src/lifecycle/copyassign/copyassign.cpp
#include <QTextStream> #include "fraction.h" int main() { QTextStream cout(stdout); Fraction twothirds(2,3); Fraction threequarters(3,4); Fraction acopy(twothirds); Fraction f4 = threequarters; cout << "after declarations - " << Fraction::report(); f4 = twothirds; cout << "\nbefore multiply - " << Fraction::report(); f4 = twothirds.multiply(threequarters); cout << "\nafter multiply - " << Fraction::report() << endl; return 0; }
<include src="src/lifecycle/copyassign/copyassign.cpp" href="src/lifecycle/copyassign/copyassign.cpp" id="copyassigncpp" mode="cpp"/>
Here is the output of this program.
copyassign> ./copyassign after declarations - [assigns: 0 copies: 2 ctors: 2] before multiply - [assigns: 1 copies: 2 ctors: 2] after multiply - [assigns: 2 copies: 3 ctors: 3] copyassign>
Question | |
---|---|
As you can see, the call to
The two copy constructors are a result of passing and returning objects as parameters to functions. Because objects are passed by value, the 2-arg constructor is from the |
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |