I. C + + class reference members, constant members of classes, initialization of static members of a class
The reference member and constant member initialization of the class are placed in the initialization list, because initialization is only one time and needs to be initialized before the constructor.
The constructor for the class is written with an error.
The static variable of a class is defined outside the class declaration, because the static variable belongs to the entire class, not to the class object, so it cannot be placed in the construction of the class
function.
As an example:
A class has both a constant member and a reference member.
classmyclass{ Public: MyClass (intValue): A (1), B (a), M_nvalue (value), C (M_nvalue), D (m_nvalue) {} Public: Const intA; Const int&b; int&C; Const int&D; intM_nvalue; StaticTMS S; Static DoubleF;}; TMS Myclass::s;DoubleMyclass::f =0.33;
The reference member of the class must be initialized in the initialization list of the class and must be initialized, or the compilation will fail.
The static member of the class needs to be defined outside the class, or it will be used with an error.
The difference between a common reference and a reference to a class is
1 You can modify the value of a reference object by reference, and you cannot modify the value of a reference object by using a common reference
2 references can change the referenced object, thereby referencing another variable, and a reference to the object cannot be changed.
3 A constant reference can reference a very variable (variable) and a constant and an rvalue, and references can only reference variables.
4 when referencing a variable, you can modify the value of the variable, and the commonly quoted value will change, but not
Modify the regular reference directly.
Here is the test
The Two class of the common object call interface must be constant function, many people do not pay attention to compile error.
The code example for a project of its own, msgstream as a constant reference, then his internal list of returned iterators need to be const_iterator
Const Msgstream & Msgstream) { = msgstream.m_ncount; MsgStream.m_listConMsg.begin (); for (std::list<conmsgnode>::const_iterator msgiter = msgStream.m_listConMsg.begin (); ! = MsgStream.m_listConMsg.end (); msgiter++ ) { m_listconmsg.push_back (*msgiter); } }
Msgstream This constant reference can only invoke a constant member function, which has a const-declared function after the body
The order of the three destructors, the order of the constructors
1 when class subclasses contain other classes, the order in which they are constructed is to construct the base class object first, then construct the class object that the child class contains, and finally construct the subclass object.
2 when a base class contains other classes, the order in which it is constructed is to first construct the class object contained in the base class, then construct the base class object, and finally construct the subclass object.
3 when a class subclass contains other classes, the order of the destructors is the first child class object, then the other class objects that the subclass contains, and the base class is finally destructor.
4 when the base class contains other classes, the order of destruction is to first destructor the subclass object, then the base class object, and finally the other objects in the base class.
The following code is written by someone else
classc{ Public: C () {printf ("c\n"); }protected:Private:};classB { Public: B () {printf ("b\n");}protected: c C;Private:};classA: Publicb{ Public: A () {printf ("a\n"); }protected:Private:};intMain () {a A; GetChar ();}
Results:
classc{ Public: C () {printf ("c\n"); }protected:Private:};classB { Public: B () {printf ("b\n");}protected:Private:};classA: Publicb{ Public: A () {printf ("a\n"); }protected: c C;Private:};intMain () {a A; GetChar ();}
Results:
classc{ Public: C () {}~c () {printf ("c\n"); }protected:Private:};classB { Public: B () {}~b () {printf ("b\n");}protected:Private:};classA: Publicb{ Public: A () {}~a () {printf ("a\n"); }protected: c C;Private:};intMain () {a A; return 0;}
Results:
Four The list is assigned directly to another list and does not cause a deep copy of the list element, and the elements in the two list are the same data
My public number, thank you for your attention:
C + + Considerations