2nd structure/destructor/assignment operation
Constructors,destructors,and Assignment Operator
Almost every class will have one or more constructors, a destructor, and a copy assignment operator.
Article 05: Understand what functions C + + silently writes and calls
Know What functions C + + silently writes and calls
When is empty class no longer an empty class ?When
after C + + has processed it. If you do not declare it, the compiler declares a copy constructor for it, a copy assignment operator, and a destructor . In addition, if no constructors are declared, the compiler declares a the default constructor. All of these functions are public and inline.So, if you write down:
Class Empty {};
It's like writing a code like this:
Class Empty {public: empty () {...} Default constructor empty (const empty &RHS) {...} Copy constructor ~empty () {...} destructor empty& operator= (const Empty &RHS) {...} Copy assignment operator};
Only these functions are called, and they are created by the compiler. It is common to need them in the program. The following code causes each of these functions to be output by the compiler:
Empty E1; Default constructor empty E2 (E1); Copy constructor e2 = E1; Copy assignment operator
The compiler writes these functions, what do these functions do?
The default constructor and destructor are primarily for the compiler to place a "hide behind the scenes" code, such as constructors and destructors that call base classes and Non-static member variables. The destructor generated by the compiler is a non-virtual unless the class's base class itself declares a virtual destructor.
As for
copy constructor and copy assignement operator, the version created by the compiler is simply
Copy each non-static member variable from the source object to the target object. Consider a namedobject template, which allows you to associate a name and an object of type T:
Templat <typename t>class namedobject {public: namedobject (const char *name, const T &value); Namedobject (const std::string &name, const T &value);p rivate: std::string namevalue; T ObjectValue;};
Because a constructor is declared, the compiler no longer creates a default constructor for it. This means that if you design a class whose constructors require arguments, you do not have to worry about the compiler adding a non-argument constructor without worrying about it and masking out its version.
Namedobject does not declare the copy constructor, nor does it declare the copy assignment operator, so the compiler creates those functions for it. Now look at the use of the copy constructor:
namedobject<int> No1 ("Smallest Prime number", 2); Namedobject No2 (No1); Call the copy constructor
compiler-generated copy constructorNo2.namevalue and No2.objectvalue must be set with No1.namevalue and no1.objectvalue as initial values. In both, the type of Namevalue is string, and the standard string has a copy constructor , so No2.namevalue is initialized in a way that
call the copy constructor of string and take no1.namevalue as the argumentAnother member of the Namedobject<int>::objectvalue type is int, which is a built-in type, so No2.objectvalue will "
copy each bits within the No1.objectvalue to complete the initialization.
The copy assignment operator generated by the compiler for namedobject<int> behaves essentially like the copy constructor, but generally only when the generated code is legitimate and has the appropriate opportunity to prove it is meaningful, The performance will be as described earlier. If a condition is not met, the compiler refuses to generate operator= for class.
For example, Namedobject is defined as follows, where Namevalue is a reference to String,objectvalue is a const T:
Template <class t>class namedobject {public: namedobject (std::string& name, const t& value);p rivate: std::string& namevalue; Const T ObjectValue;};
Now consider what happens next:
std::string Newdog ("Persephone"); std::string olddog ("Satch"); Namedobject<int> p (Newdog, 2); namedobject<int> s (odldog,;p) = s;
Before assignment, both P.namevalue and S.namevalue point to the string object. How does the assignment affect the P.namevalue? After the assignment, should the P.namevalue point to the string that the S.namevalue refers to?
In the face of this problem,
the response of C + + is to refuse to compile the line assignment action. If you plan to support assignment operations within a class containing reference members, you must customize the copy assignment operator.The compiler reacts to the classes with the "Const member".
Note:
The compiler can secretly create a default constructor for class, a copy constructor, a copy assignment operator, and a destructor.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Effective c++--Clause 5 (chapter 2nd)