目录
初始化只能初始化一次,而构造函数体内可以多次赋值
初始化格式:
- class Date
- {
- public:
- Date(int year, int month, int day)
- : _year(year)
- , _month(month)
- , _day(day)
- {}
- private:
- int _year;
- int _month;
- int _day;
- };
注意:
1、只能初始化一次
2、类中包含以下成员,必须放在初始化列表位置进行初始化:
引用成员变量
const成员变量
自定义类型成员
- class A
- {
- public:
- A(int a)
- :_a(a)
- {}
- private:
- int _a;
- };
- class B
- {
- public:
- B(int a, int ref)
- :_aobj(a)
- , _ref(ref)
- , _n(10)
- {}
- private:
- A _aobj; // 没有默认构造函数
- int& _ref; // 引用
- const int _n; // const
- };
3、自定义类型建议先使用初始化列表初始化
- class Time
- {
- public:
- Time(int hour = 0)
- :_hour(hour)
- {
- cout << "Time()" << endl;
- }
- private:
- int _hour;
- };
- class Date
- {
- public:
- Date(int day)
- {}
- private:
- int _day;
- Time _t;//Time _t为自定义类型
- };
- int main()
- {
- Date d(1);
- }
explicit修饰构造函数,禁止类型转换
- class Date
- {
- public:
- Date(int year)
- :_year(year)
- {}
- private:
- int _year;
- };
- Date d1(2022);//创建对象
- Date d2 = 2022;//隐式类型的转换,用整型构造Date的临时对象时,再用对象去拷贝构造d2
- const Date& d5 = 2022;//隐式类型转换
- Date d3(d1);//拷贝构造
- Date d3 = d1;//拷贝构造
- //构造+拷贝构造Date d1={2022,10,13}等价于Date d2(2022,10,12);
- class A
- {
- public:
- A()
- {
- ++_second;
- }
- A(const A& t)
- {
- ++_second;
- }
- ~A()
- {
- --_second;
- }
- static int GetACount()
- {
- return _second;//没有this指针,只能访问静态成员
- }
- private:
- static int _second;//声明
- };
- int A::_second = 0;//定义初始化
- void TestA()
- {
- cout << A::GetACount() << endl;
- A a1, a2;
- A a3(a1);
- cout << A::GetACount() << endl;
- }
特性:
1. 静态成员为所有类对象所共享,不属于某个具体的对象,存放在静态区
2. 静态成员变量必须在类外定义,定义时不添加static关键字,类中只是声明
3. 类静态成员即可用 类名::静态成员 或者 对象.静态成员 来访问
4. 静态成员函数没有隐藏的this指针,不能访问任何非静态成员
5. 静态成员也是类的成员,受public、protected、private 访问限定符的限制
友元函数可访问类的私有和保护成员,但不是类的成员函数
友元函数不能用const修饰
友元函数可以在类定义的任何地方声明,不受类访问限定符限制
一个函数可以是多个类的友元函数
友元函数的调用与普通函数的调用原理相同
- class Date
- {
- friend ostream& operator<<(ostream& _cout, const Date& d);
- friend istream& operator>>(istream& _cin, Date& d);
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- : _year(year)
- , _month(month)
- , _day(day)
- {}
- private:
- int _year;
- int _month;
- int _day;
- };
- ostream& operator<<(ostream& _cout, const Date& d)
- {
- _cout << d._year << "-" << d._month << "-" << d._day;
- return _cout;
- }
- istream& operator>>(istream& _cin, Date& d)
- {
- _cin >> d._year;
- _cin >> d._month;
- _cin >> d._day;
- return _cin;
- }
- int main()
- {
- Date d;
- cin >> d;
- cout << d << endl;
- return 0;
- }
友元关系是单向的,不具有交换性。:比如上述Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。
友元关系不能传递:如果C是B的友元, B是A的友元,则不能说明C时A的友元。
友元关系不能继承,在继承位置再给大家详细介绍。
- class Time
- {
- friend class Date; // 声明日期类为时间类的友元类,则在日期类中就直接访问Time类中的私有成员变量
- public:
- Time(int hour = 0, int minute = 0, int second = 0)
- : _hour(hour)
- , _minute(minute)
- , _second(second)
- {}
- private:
- int _hour;
- int _minute;
- int _second;
- };
- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- : _year(year)
- , _month(month)
- , _day(day)
- {}
- void SetTimeOfDate(int hour, int minute, int second)
- {
- // 直接访问时间类私有的成员变量
- _t._hour = hour;
- _t._minute = minute;
- _t._second = second;
- }
- private:
- int _year;
- int _month;
- int _day;
- Time _t;
- };
- class A
- {
- public:
- A(int a = 0)
- :_a(a)
- {
- cout << "A(int a)" << endl;
- }
- ~A()
- {
- cout << "~A()" << endl;
- }
- private:
- int _a;
- };
- class Solution
- {
- public:
- int Sum_Solution(int n)
- {
- return n;
- }
- };
- int main()
- {
- A aa1;
- A();//匿名对象,生命周期是当前这一行
- A(3);//匿名对象,生命周期是当前这一行
- A aa2();
- Solution().Sum_Solution(10);
- return 0;
- }
- //A aa1();这样定义是错误的,因为不能区分是声明还是定义
在传参和传返回值的过程中,一般编译器会做一些优化,减少对象的拷贝,这个在一些场景下还
是非常有用的。
- class A
- {
- public:
- A(int a = 0)
- :_a(a)
- {
- cout << "A(int a)" << endl;
- }
- A(const A& aa)
- :_a(aa._a)
- {
- cout << "A(const A& aa)" << endl;
- }
- A& operator=(const A& aa)
- {
- cout << "A& operator=(const A& aa)" << endl;
- if (this != &aa)
- {
- _a = aa._a;
- }
- return *this;
- }
- ~A()
- {
- cout << "~A()" << endl;
- }
- private:
- int _a;
- };
- void f1(A aa)
- {}
- A f2()
- {
- A aa;
- return aa;
- }
- int main()
- {
- // 传值传参
- A aa1;
- f1(aa1);
- cout << endl;
-
- // 传值返回
- f2();
- cout << endl;
-
- // 隐式类型,连续构造+拷贝构造->优化为直接构造
- f1(1);
-
- // 一个表达式中,连续构造+拷贝构造->优化为一个构造
- f1(A(2));
- cout << endl;
-
- // 一个表达式中,连续拷贝构造+拷贝构造->优化一个拷贝构造
- A aa2 = f2();
- cout << endl;
-
- // 一个表达式中,连续拷贝构造+赋值重载->无法优化
- aa1 = f2();
- cout << endl;
-
- return 0;
- }