Valid C ++ clause 5 and valid clause 5
Understand which functions are written and called by C ++ by default
This section contains two knowledge points.
First
For a class
1. If no constructor exists in the class, the compiler declares a default constructor for the class.
2. If the class has no destructor, copy constructor, or copy assignment operator, the class declares the three functions.
3. the compiler generates the destructor, copy constructor, and copy assignment operators only. The declaration and generation are two different codes.
The following code:
// Defined classclass Empty {}; // classclass Empty {public: Empty () {} Empty (const Empty & rhs) declared by the compiler) {} Empty & operator = (const Empty & rhs ){}~ Empty (){}};
Second
If data of the reference type or const type exists in the class. The compiler rejects the automatic generation of the copy assignment operator function. Unless you define
The following code:
// For this class, the compiler will not automatically generate the copy assignment template <typename T> class NamedObject {public: NamedObject (const char * name, const T & value); NamedObject (const std:: string & name, const T & value); private: std: string & nameValue; const T objectValue ;};