-
explicit
keyword in constructor means that the constructor can be called with exactly one argument. -
Default constructor
A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible.
-
Any change to the default argument values of a function requires the client code to be recompiled (to ensure that the program still functions correctly).
-
A constructor can call other member functions
-
Making data members
private
and controlling access, especially write access, to those data members throughpublic
member functions helps ensure data integrity. -
C++11 allows constructors to call other constructors in the same class. The calling constructor is known as a delegating constructor—it delegates its work to another constructor.
-
Every class has exactly one destructor. If you do not explicitly define a destructor, the compiler defines an “empty” destructor.
-
Constructors and destructors are called implicitly when object are created and when they’re about to be removed from memory. Destructors are called in reversed order. However, the global and static objects can alter the order in which destructors are called.
-
Returning a reference or a pointer to a
private
data member breaks the encapsulation of the class and makes the client code dependent on the representation of the class’s data. -
The assignment operator (
=
) can be used to assign an object to another object of the same class. By default, such assignment is performed by memberwise assignment (also called copy assignment) —- each data member of the object on the right of the assignment operator is assigned individually to the same data member in the object on the left of the assignment operator. -
Copy constructor, like copy assignment, copy each data member to another object. For each class, the compiler provides a default copy constructor that copies each member of the original object into the corresponding member of the new object. Like memberwise assignment, copy constructors can cause serious problems when used with a class whose data members contain pointers to dynamically allocated memory.
A copy constructor of class T is a non-template constructor whose first parameter is
T&
,const T&
,volatile T&
, orconst volatile T&
, and either there are no other parameters, or the rest of the parameters all have default values. -
const
object andconst
member function. -
Data members are constructed in the order in which they’re declared in the class definition (not in the order they’re listed in the constructor’s member-initializer list) and before their enclosing class objects are constructed.
-
Destructor of an object is from outside in, and the data member destructor in reverse order, the data member declared last is the first one to destruct.
-
If a member object is not initialized through a member initializer, the member object’s default constructor will be called implicitly.
-
Initialize member objects explicitly through member initializers. This eliminates the overhead of “doubly initializing” member objects—once when the member object’s default constructor is called and again when set functions are called in the constructor body (or later) to initialize the member object.
-
A compilation error occurs if a member object is not initialized with a member initializer and the member object’s class does not provide a default constructor (i.e., the member object’s class defines one or more constructors, but none is a default constructor).
-
friend
Functions andfriend
ClassesA
friend
function of a class is a non-member function that has the right to access thepublic
and non-public
class members. Standalone functions, entire classes or member functions of other classes may be declared to be friends of another class. -
The
friend
declaration(s) can appear anywhere in a class and are not affected by access specifiers
public
orprivate
orprotected
. -
Friendship is not symmetric, and friendship is not transitive.
-
Even though the prototypes for
friend
functions appear in the class definition, friends are not member functions. -
Place all friendship declarations first inside the class definition’s body and do not precede them with any access specifier.
-
Type of
this
pointer:- In a non-const member function of class
Employee
, thethis
pointer has the typeEmployee* const
—- a constant pointer to a non-constant Employee. - In a
const
member function, this has the typeconst Employee* const
—- a constant pointer to a constant Employee .
- In a non-const member function of class
-
Another use of the
this
pointer is to enable cascaded member-function calls -— that is, invoking multiple functions sequentially in the same statement. Usereturn *this;
in the setter function to enable cascaded member-function calls. -
Use
static
data members to save storage when a single copy of the
data for all objects of a class will suffice—such as a constant that can be shared by all objects of the class. -
A class’s
static
data members have class scope. A static data member must be initialized exactly once. In C++11, allstatic const
data members can have in-class initializers. If a static data member is an object of a class that provides a default constructor, thestatic
data member need not be initialized because its default constructor will be called. -
静态数据区的变量都有默认的初始值,而动态数据区(堆区、栈区)的变量默认是垃圾值。
-
If a member function is defined in a class’s body, the member function is implicitly declared inline. Remember that the compiler reserves the right not to inline any function.
-
Defining a member function inside the class definition inlines the member function (if the compiler chooses to do so). This can improve performance.
-
Only the simplest and most stable member functions (i.e., whose implementations are unlikely to change)should be defined in the class header, because every change to the header requires you to recompile every source-code file that’s dependent on that header (a time-consuming task in large systems).
-
Inline functions
C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality. Normal functions do not have any such functionality. Inline function usually make the code faster in execution due to less number of function which require System Stack access.