Details
1. Constructors aspects
Initialization list
in the constructor at the beginning with a colon followed by a comma-separated list of data members, a member variable after each parentheses, brackets internal use similar copy configured to initialize member variables.
class Date
{
public:
Date(int year = 2019, int month = 10, int day = 19)//构造函数
:_year(year)//初始化列表
,_month(month)
, _day(day)
{
}
Date(Date& d)//拷贝构造函数
:_year(d._year)//初始化列表
,_month(d._month)
,_day(d._day)
{
}
private:
int _year;
int _month;
int _day;
};
note:
- Each member variable initialization list can only be initiated once .
- Class contains about members must be initialized in the initialization list.
const member variables modified
reference type member variables of
a class type (custom type) member variable (custom class and no default constructor)
Example 1
class B
{
public:
private:
int& _ref;
const int _b;
};
int main()
{
B b;
return 0;
}
When the direct object is instantiated b, it reported a mistake?
When const member variables when B & types exist or directly report an error.
Example 2
class A
{
public:
A(int a)
:_a(a)
{}
private:
int _a;
};
class B
{
public:
private:
A _ca;
};
int main()
{
B b;
return 0;
}
It also appeared on the error:
When the object class B Example B, since the object B exists in class A, so that when the object is instantiated, if no appropriate constructor for object B in _a call can also occur this type of error.
Solution
use a list initialized to these can not be resolved correctly instantiated three kinds that occur when an object is instantiated.
const: non-modifiable variables, so when you create must be initialized;
& Type: When you create a reference type must be referenced to a variable, otherwise no way to be created.
Custom type: If no custom types callable appropriate constructor call of a custom type object can not be instantiated.
class A
{
public:
A(int a)
:_a(a)
{}
private:
int _a;
};
class B
{
public:
B(A ca, int ref, int b)//使用初始化列表
:_ca(ca)
,_ref(ref)
,_b(b)
{}
private:
A _ca;
int& _ref;
const int _b;
};
int main()
{
B b(1, 2, 3);//传递列表中的参数
return 0;
}
First custom class _ca be constructed, in the call initialization list const, int & initialization together.
note
- Try to use initialization list to initialize , because the member variables to customize the type, use initialization list to initialize.
- In the member variable is initialized in the initialization list in the order of the classes in the order of declaration , irrespective of their order in the initialization list.
class B
{
public:
B(int a, int b)
: _b(b)//先初始化成员变量_a
, _a(a)//与所放置位置无关
{
}
private:
int _a;
int _b;
};
Construction unnamed objects
When instantiate an object that only one parameter can be assigned directly by the symbol =.
class B
{
public:
B(int a) :_a(a)
{
}
private:
int _a;
};
int main()
{
B b1(1);
b1 = 20;
return 0;
}
In b1 (1); time, b1 _a first member variables to 1;
although b1 = 20; direct type B 20 is configured to a nameless objects , to perform the assignment of b1.
2. Static member
Static member variables
- Static member variables at initialization, initialization can not be placed in position in the list. Outside the class must be initialized .
- Static member variables can be thought of as a member variable outside the class, all objects share the static member variables .
class B
{
public:
B()
{
}
private:
static int _s;
};
int B::_s = 10;// 在类外进行初始化静态成员变量
int main()
{
B b;
return 0;
}
Static member function
- Static member function declared in class, outside of class can be achieved, can also be achieved in the class.
class B
{
public:
static void Func1();
static void Func2()
{
cout<<"static Func2"<<endl;
}
private:
int _a;
};
void B::Fun1c1()//在类外实现也和静态成员变量相同,
{ //必须的加上作用域限定符B
cout << "static Func1" << endl;
}
int main()
{
B b;
b.Func1();
b.Func2();
}
- Static member function does not contain this pointer
class B
{
public:
static void Func()
{
_a = 10;//在类内静态成员函数调用this指针中的变量_a
}
private:
int _a;
};
int main()
{
B b;
b.Func();
}