“ Ctor, Dtor, and Assignment,” The Practice of Programming, CSIE@NTNU, 2010 55 Explicitly Disallow Undesired Compiler-generated Functions. As mentioned, the compiler generates copy constructor and copy assignment automatically for us. In some cases, however, we do not want them. class CPlayer { public: CPlayer() { uniqueID_ = NameDatabase_.GetID(); } // ... private: static CNameDatabase NameDatabase_; int uniqueID_; // ... }; int main() { CPlayer p1, p2(p1), p3; p3 = p1; }
21
Embed
Explicitly Disallow Undesired Compiler-generated Functions.web.ntnu.edu.tw/~tcchiang/tpp/3_Ctor, dtor, and assignment (III).pdf · “Ctor, Dtor, and Assignment,”The Practice of
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
“Ctor, Dtor, and Assignment,”The Practice of Programming, CSIE@NTNU, 201055
Explicitly Disallow UndesiredCompiler-generated Functions. As mentioned, the compiler generates copy
constructor and copy assignment automatically for us.In some cases, however, we do not want them.
// Do not define copy constructor & copy assignment
};
FAQ1: Why not public or private?
FAQ2: Why not public?
FAQ1: Because we do not want to define Uncopyable objects but want to define CPlayer objects.FAQ2: private inheritance means “is implemented in terms of”, where public inheritance means “is a”.
“Ctor, Dtor, and Assignment,”The Practice of Programming, CSIE@NTNU, 201060
Ensure your class provides correct copying actions,you have the following options: Explicitly disable copying. Explicitly define your own copying functions. Use the compiler-generated version, AND use comments to
let readers know that you do not ignore the above twooptions carelessly.
Note: Copying & standard containers Disable copying implies that you can not put your objects
into standard containers. However, you can still simulate it through the aid of (smart)
pointers.
“Ctor, Dtor, and Assignment,”The Practice of Programming, CSIE@NTNU, 201061
Prefer the Canonical Form ofAssignment. Let your operator= be a member function with one
of the following two signatures*:T& operator=(const T&); // classic
T& operator=(T); // if you want a copy of the argument
Return a reference to *this. Don’t return const T&.
Copy all parts of an object.
Handle assignment to self. Avoid making your assignment operator virtual.
If you really need it, prefer to provide a named functioninstead (e.g. virtual void Assign(const T&); )
* An exception is std::auto_ptr. (See item 10 in C++ Gotchas.)
“Ctor, Dtor, and Assignment,”The Practice of Programming, CSIE@NTNU, 201062
Prefer the Canonical Form ofAssignment.Return a reference to *this. Don’t return const T&.class CPlayer {public:
me = me; // I am not that stupid.players[i] = players[j]; // Am I sure that i j?*ptr1 = *ptr2; // Am I sure that ptr1 ptr2?ref = *ptr1; // Am I sure that ref *ptr1?
return 0;}
“Ctor, Dtor, and Assignment,”The Practice of Programming, CSIE@NTNU, 201069
Prefer the Canonical Form ofAssignment.Handle assignment to self.
In principle, the swap() function should be no-fail.
If self-assignment is frequent due to reference aliasing or other reasons, it’s okay to still checkfor self-assignment anyway as an optimization check to avoid needless work.
“Ctor, Dtor, and Assignment,”The Practice of Programming, CSIE@NTNU, 2010
Exercise
Add more operators to your simulated Stringclass.
class String{public:
// 1. default constructor// 2. copy constructor// 3. constructor with one parameter with type const char *// 4. destructor// 5. size()// 6. c_str()// 7. copy assignment// 8. operator []// 9. operator +=