目录
- 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)
- :_a1(a)
- ,_a2(_a1)
- {}
-
- void Print() {
- cout<<_a1<<" "<<_a2<<endl;
- }
- private:
- int _a2;
- int _a1;
- };
-
- int main()
- {
- A aa(1);
- aa.Print();
- }
运行结果:

为什么?因为上述代码是_a2首先声明,所以先初始化_a2,但是此时_a1是随机值
- 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;
- };
-
- void Test()
- {
- Date d1(2022);
- // 用一个整形变量给日期类型对象赋值
- // 实际编译器背后会用2023构造一个无名对象,最后用无名对象给d1对象进行赋值
- d1 = 2023;
- // 将1屏蔽掉,2放开时则编译失败,因为explicit修饰构造函数,禁止了单参构造函数类型转
- 换的作用
- }
- int count = 0;
-
- class A
- {
- public:
- A(int a = 1)
- {
- ++count;
- }
-
- A(const A& aa)
- {
- ++count;
- }
- };
-
- void Func(A a)
- {}
-
- int main()
- {
- A aa1;
- A aa2(aa1);
-
- Func(aa1);
-
- A aa3 = 1;
-
- cout << count << endl;
-
- return 0;
- }
使用static成员:

- 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;
- };
- 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;
- }
友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。
- 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
- {
- 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)" << endl;
- }
-
- ~A()
- {
- cout << "~A()" << endl;
- }
-
- private:
- int _a;
- };
-
-
- class Solution
- {
- public:
- int Sum_Solution(int n)
- {
- //...
- return n;
- }
- };
-
-
- int main()
- {
- A aa1;
- //A aa1();
- // 不能这么定义对象,因为编译器无法识别下面是一个函数声明,还是对象定义
-
-
- // 但是我们可以这么定义匿名对象,匿名对象的特点不用取名字,
- // 但是他的生命周期只有这一行,下一行它就会自动调用析构函数
- A();
- Solution();
-
- // 使用场景
- cout << Solution().Sum_Solution(10) << endl;
-
-
- return 0;
- }
- 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;
- };

注:概念性内容均来自比特科技