Understand what functions are written and called by C + + by default
This section has two points of knowledge.
First
For a class
1. If there is no constructor in class, the compiler declares a default constructor for class.
2. If class does not have a destructor, a copy constructor, and a copy assignment operator, then class declares these three functions.
3. The compiler generates them only when destructors, copy constructors, and copy assignment operators are called, so let's be clear that declarations and builds are separate things.
The following code:
//定义的classclass Empty{};//编译器声明的classclass Empty{public: Empty(){} Empty(constEmpty& rhs){} Empty& operator=(constEmpty& rhs){} ~Empty(){}};
Secondly
When there is a reference type of data or const type data in the class. The compiler refuses to automatically generate the copy assignment operator function. Unless you define it yourself.
The following code:
//对于这个类,编译器不会自动生成copy assignment template<typename T>class NamedObject{public: NamedObject(constcharconstvalue); NamedObject(const std::stringconstvalue);private: std::string& nameValue; const T objectValue;};
Effective C + + clause 5