[ fromfile: copyassign.xml id: copyassign ]
A copy constructor is a constructor that has a prototype like this:
ClassName(const ClassName & x);
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);
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; }; [ . . . . ]
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; } [ . . . . ]
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; }
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 |
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |