在创建对象时,编译器通过调用构造函数,给对象中各个成员变量一个合适的初始值。
- class Date
- {
- public:
- Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
虽然上述构造函数调用之后,对象中已经有了一个初始值,但是不能将其称为对对象中成员变量的初始化,构造函数体中的语句只能将其称为赋初值,而不能称作初始化。因为初始化只能初始化一次,而构造函数体内可以多次赋值。
初始化列表:以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个"成员变量"后面跟一个放在括号中的初始值或表达式。
- class Date
- {
- public:
- Date(int year, int month, int day)
- : _year(year)
- , _month(month)
- , _day(day)
- { }
- private:
- int _year;
- int _month;
- int _day;
- };
【注意】
- class A
- {
- public:
- A(int a = 0)
- :_a(a)
- { }
- private:
- int _a;
- };
-
- class B
- {
- public:
- B(int a, int& ref)
- :_aobj(10)
- ,_ref(ref)
- ,x(2)
- ,_n(10)
- { }
- private:
- A _aobj; // 没有默认构造函数
- int& _ref; // 引用
- const int _n; // const
- int x = 1;
- };
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;
- };
-
- int main()
- {
- Date d(1);
- return 0;
- }
4. 成员变量在类中声明次序就是其在初始化列表中的初始化顺序,与其在初始化列表中的先后次序无关。
- class A
- {
- public:
- A(int a)
- :_a1(a)
- ,_a2(_a1)
- { }
- void Print()
- {
- cout<<_a1<<" "<<_a2<
- }
- private:
- int _a2;
- int _a1;
- };
-
- int main()
- {
- A aa(1);
- aa.Print();
- return 0;
- }
-
- //输出1 随机值
3、explicit关键字
构造函数不仅可以构造与初始化对象,对于单个参数或者除第一个参数无默认值其余均有默认值的构造函数,还具有类型转换的作用。
- class Date
- {
- public:
- // 1. 单参构造函数,没有使用explicit修饰,具有类型转换作用
- // explicit修饰构造函数,禁止类型转换---explicit去掉之后,代码可以通过编译
- explicit Date(int year)
- :_year(year)
- { }
- /*
- // 2. 虽然有多个参数,但是创建对象时后两个参数可以不传递,没有使用explicit修饰,具有类型转
- 换作用
- // explicit修饰构造函数,禁止类型转换
- explicit Date(int year, int month = 1, int day = 1)
- : _year(year)
- , _month(month)
- , _day(day)
- { }
- */
- Date& operator=(const Date& d)
- {
- if (this != &d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
- return *this;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- int main()
- {
- Date d1(2022);
- // 用一个整形变量给日期类型对象赋值
- // 实际编译器背后会用2023构造一个无名对象,最后用无名对象给d1对象进行赋值
- d1 = 2023;
- // 将1屏蔽掉,2放开时则编译失败,因为explicit修饰构造函数,禁止了单参构造函数类型转换的作用
- return 0;
- }
上述代码可读性不是很好,用explicit修饰构造函数,将会禁止构造函数的隐式转换。
static成员
1、概念
声明为static的类成员称为类的静态成员,用static修饰的成员变量,称之为静态成员变量;用static修饰的成员函数,称之为静态成员函数。静态成员变量一定要在类外进行初始化。
面试题:实现一个类,计算程序中创建出了多少个类对象。
- class A
- {
- public:
- A() { ++_scount; }
- A(const A& t) { ++_scount; }
- ~A() { --_scount; }
- static int GetACount() { return _scount; }
- private:
- static int _scount;
- };
- int A::_scount = 0;
- int main()
- {
- cout << A::GetACount() << endl;
- A a1, a2;
- A a3(a1);
- cout << A::GetACount() << endl;
- return 0;
- }
2、特性
- 静态成员为所有类对象所共享,不属于某个具体的对象,存放在静态区。
- 静态成员变量必须在类外定义,定义时不添加static关键字,类中只是声明。
- 类静态成员即可用 类名::静态成员 或者 对象.静态成员 来访问。
- 静态成员函数没有隐藏的this指针,不能访问任何非静态成员。
- 静态成员也是类的成员,受public、protected、private 访问限定符的限制。
【问题】
1. 静态成员函数不可以调用非静态成员函数。
2. 非静态成员函数可以调用类的静态成员函数。
友元
友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。
友元分为:友元函数和友元类
1、友元函数
问题:现在尝试去重载operator<<,然后发现没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作数了。但是实际使用中cout需要是第一个形参对象,才能正常使用。所以要将operator<<重载成全局函数。但又会导致类外没办法访问成员,此时就需要友元来解决。operator>>同理。
- class Date
- {
- public:
- Date(int year, int month, int day)
- : _year(year)
- , _month(month)
- , _day(day)
- { }
- // d1 << cout; -> d1.operator<<(&d1, cout); 不符合常规调用
- // 因为成员函数第一个参数一定是隐藏的this,所以d1必须放在<<的左侧
- ostream& operator<<(ostream& _cout)
- {
- _cout << _year << "-" << _month << "-" << _day << endl;
- return _cout;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但需要在类的内部声明,声明时需要加friend关键字。
- 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;
- }
说明:
- 友元函数可访问类的私有和保护成员,但不是类的成员函数。
- 友元函数不能用const修饰。
- 友元函数可以在类定义的任何地方声明,不受类访问限定符限制。
- 一个函数可以是多个类的友元函数。
- 友元函数的调用与普通函数的调用原理相同。
2、友元类
友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。
- 友元关系是单向的,不具有交换性。比如上述Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。
- 友元关系不能传递,如果B是A的友元,C是B的友元,则不能说明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;
- };
内部类
概念:如果一个类定义在另一个类的内部,这个内部类就叫做内部类。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何优越的访问权限。
注意:内部类就是外部类的友元类,参见友元类的定义,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元。
特性:
- 内部类可以定义在外部类的public、protected、private都是可以的。
- 注意内部类可以直接访问外部类中的static成员,不需要外部类的对象/类名。
- sizeof(外部类)=外部类,和内部类没有任何关系。
- class A
- {
- private:
- static int k;
- int h;
- public:
- class B // B天生就是A的友元
- {
- public:
- void foo(const A& a)
- {
- cout << k << endl;//OK
- cout << a.h << endl;//OK
- }
- };
- };
-
- int A::k = 1;
-
- int main()
- {
- A::B b;
- b.foo(A());
- return 0;
- }
匿名对象
- class A
- {
- public:
- A(int a = 0)
- :_a(a)
- {
- cout<<"A(int a)"<
- }
- ~A()
- {
- cout<<"~A( )"<
- }
- pirvate:
- int _a;
- };
-
- class Solution
- {
- public:
- int Sum_Solution(int n)
- {
- cout<<"Sum_Solution"<
- return n;
- }
- };
-
- int main()
- {
- A aa(1); //有名对象--生命周期在当前函数局部域
- A(2); //匿名对象--生命周期在当前行
-
- Solution stl;
- stl.Sum_Solution(10);
-
- Solution().Sum_Solution(20);
-
- A& ra = A(1); //error
- //匿名对象具有常性,类似临时对象
-
- const A& ra = A(1); //const引用延长匿名对象的生命周期,生命周期在当前函数局部域
- }
-
相关阅读:
Python中的元素相乘与矩阵相乘(附Demo)
esp32之间通过espnow通信实现遥控电机的转速
CDN工作原理
数组篇-其之一-数组的概念与一维数组
Git之借助Commitizen规范化提交代码
linux创建用户基本操作
docker 部署 mysql8.0.30
计算机网络(一):计算机网络概念、功能、组成
Python中的三元运算符详解
思科认证和华为认证到底选择哪个更合适啊?
-
原文地址:https://blog.csdn.net/JX_BC/article/details/133967660