从C语言到C++⑦(第二章_类和对象_下篇)初始化列表+explicit+static成员+友元+内部类+匿名对象(下)

简介: 从C语言到C++⑦(第二章_类和对象_下篇)初始化列表+explicit+static成员+友元+内部类+匿名对象

从C语言到C++⑦(第二章_类和对象_下篇)初始化列表+explicit+static成员+友元+内部类+匿名对象(中):https://developer.aliyun.com/article/1513653


4.3 友元函数

友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数。

它不属于任何类,但需要在类的内部进行声明,声明时要加 friend 关键字。

我们现在就可以去解决刚才的问题了:

#include <iostream>
using namespace std;
 
class Date 
{
  friend void operator<<(ostream& out, const Date& d);// 友元的声明
 
public:
  Date(int year = 1, int month = 1, int day = 1) 
  {
    _year = year;
    _month = month;
    _day = day;
  }
  void Print() const 
  {
    cout << _year << "年" << _month << "月" << _day << "日" << endl;
  }
 
  void operator<<(ostream& out)
  {
    out << _year << "年" << _month << "月" << _day << "日" << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};
 
void operator<<(ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}
 
int main()
{
  Date d1(2023, 5, 7);
  //d1.Print();
  cout << d1;
  //d1 << cout;
 
  return 0;
}

如果我们想连续地输出呢?我想在这又输出 d1 又输出 d2。

cout << d1 << d2;

现在实现的不支持。这和连续赋值很像,只是连续赋值是从右往左,这里是从左往右。

连续插入 d1 和 d2 实际上就是两次函数的调用,这里先执行的是 cout << d1,

因为调用函数后返回值是 void,void 会做这里的左操作数,

所以当然不支持连续输出了,我们可以改一下,

我们把返回值改为 ostream 就行,把 out 返回回去。

解决了流插入,我们再来顺便实现一下流提取。

这样我们上一篇的大练习:日期类,基本上就完整了。


流提取因为要把输入的东西写到对象里去,会改变,所以这里当然不能加 const 。

istream& operator>>(istream& in, Date& d) 
{
  in >> d._year >> d._month >> d._day;
  return in;
}

4.3.1 完整流插入流提取重载:

#include <iostream>
using namespace std;
 
class Date 
{
  friend ostream& operator<<(ostream& out, const Date& d);// 友元的声明
  friend istream& operator>>(istream& in, Date& d);
 
public:
  Date(int year = 1, int month = 1, int day = 1) 
  {
    _year = year;
    _month = month;
    _day = day;
  }
  void Print() const 
  {
    cout << _year << "年" << _month << "月" << _day << "日" << endl;
  }
 
  void operator<<(ostream& out)
  {
    out << _year << "年" << _month << "月" << _day << "日" << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};
 
ostream& operator<<(ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  return out;
}
istream& operator>>(istream& in, Date& d) 
{
  in >> d._year >> d._month >> d._day;
  return in;
}
 
int main()
{
  Date d1(2023, 5, 7);
  Date d2(2023, 5, 8);
  //d1.Print();
  cout << d1 << d2;
  //d1 << cout;
 
  Date d3;
  Date d4;
  cin >> d3 >> d4;
  cout << d3 << d4 << endl;
 
  return 0;
}

4.3.2 友元函数注意事项:

① 友元函数可以访问类的 private 和 protected 成员,但并不代表能访问类的成员函数。

② 友元函数不能用 const 修饰。

③ 友元函数可以在类定义的任何地方申明,可以不受类访问限定符的控制。

④ 一个函数可以是多个类的友元函数。

⑤ 友元函数的调用和普通函数的调用原理相同。

4.4 友元类

友元类的所有成员函数都可以是另一个类的友元函数,

都可以访问另一个类中的非公有成员。

friend class 类名;

① 友元关系是单向的,不具有交换性。

② 友元关系不具有传递性(朋友的朋友不一定是朋友)。

   如果 C 是 B 的友元,B 是 A 的友元,则不能说明 C 是 A 的友元。

③ 友元关系不能继承,在后面学习继承的时候再给大家详细介绍。

定义一个友元类:

#include<iostream>
using namespace std;
 
class Date;   // 前置声明
 
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 = 1, int month = 1, int day = 1)
    : _year(year)
    , _month(month)
    , _day(day)
  {}
 
  void GetTime()
  {
    // 直接访问Time类私有的成员变量
    cout << _t._hour << ":" << _t._minute << ":" << _t._second << endl;
  }
 
private:
  int _year;
  int _month;
  int _day;
  Time _t;
};
 
int main()
{
  Date d;
  d.GetTime();
 
  return 0;
}

这里 Date 是 Time 的友元,我们在日期类里就可以访问时间类的私有成员了。

但是时间类里不能访问日期类,因为这是 "单向好友" ,

如果想在时间类里访问日期类,我们可以在日期类里声明:

class Date 
{
    friend class Time;
    // ...
}

这样,它们之间就是 "双向好友" 了 -> 互相成为对方的友元。

5. 内部类(了解)

C++中不常用内部类,Java中用得多一点,所以我们了解一下就行。

5.1 内部类的概念

如果在 A 类中定义 B 类,我们称 B 是 A 的内部类。

class A 
{
  class B {
    ;
  };
};

内部类是一个独立的类, 它不属于外部类,更不能通过外部类的对象去访问内部类的成员。

外部类对内部类没有任何优越的访问权限。

注意: 内部类就是外部类的友元类 ,(我把你放在我心里,你就是我的友元)

参见友元类的定义,内部类可以通过外部类的对象参数来访问外部类中的所有成员。

但是外部类不是内部类的友元。

#include<iostream>
using namespace std;
 
class A
{
public:
  class B // B天生就是A的友元
  {
  public:
    void fuc(const A& a)
    {
      cout << g << endl;
      cout << a.r << endl;
    }
  };
 
private:
  static int g;
  int r = 19;
};
 
int A::g = 1;
 
int main()
{
  A a;
  A::B b;
  b.fuc(a);
 
  return 0;
}

5.2 内部类的特性

1. 内部类可以定义在外部类的public、protected、private都是可以的。

2. 注意内部类可以直接访问外部类中的static成员,不需要外部类的对象/类名。

   (同上代码,加上外部类的对象/类名也行)

3. sizeof(外部类)=外部类,和内部类没有任何关系。

#include<iostream>
using namespace std;
 
class A 
{
private:
  static int _s_a1;
  int _a2;
 
public:
  class B 
  {
  private:
    int _b1;
  };
};
 
int A::_s_a1 = 1;
 
int main()
{
  cout << "A的大小为: " << sizeof(A) << endl;
 
  return 0;
}

sizeof(外部类)=外部类,和内部类没有任何关系。

内部类 B 天生就是外部类 A 的友元,也就是 B 中可以访问 A 的私有(或保护),

A 不能访问 B 的私有(或保护)。

所以,A 类型的对象里没有 B,跟 B 没什么关系,计算 sizeof 当然也不会带上B。

加上静态成员属于整个类,是放在静态区的,所以这里只计算了int _a2的大小。

6. 匿名对象

匿名对象是指创建对象时,只有创建对象的语句,却没有把对象地址值赋值给某个变量。


产生匿名对象的三种情况:


① 以值的方式给函数传参;

 A(); —> 生成了一个匿名对象,执行完Cat( )代码后,此匿名对象就此消失。这就是匿名对象的生命周期。

  A aa = A(); —>首先生成了一个匿名对象,然后将此匿名对象变为了aa对象,其生命周期就变成了aa对象的生命周期。


② 类型转换;


③ 函数需要返回一个对象时;return temp;

#include<iostream>
using namespace std;
 
class A
{
public:
  A(int a = 0)
    :_a(a)
  {
    cout << "A(int a)" << endl;
  }
 
  ~A()
  {
    cout << "~A()" << endl;
  }
 
private:
  int _a;
};
 
int main()
{
  A aa1;
  //A aa1(); 不能这么定义对象,因为编译器无法识别下面是一个函数声明,还是对象定义
  // 但是我们可以这么定义匿名对象,匿名对象的特点不用取名字,
  // 但是他的生命周期只有这一行,我们可以看到下一行他就会自动调用析构函数
  A();
  A aa2;
 
  return 0;
}

7. 拷贝对象时的一些编译器优化

在传参和传返回值的过程中,新一点的主流的编译器都会做一些优化,减少对象的拷贝,

这个在一些场景下还是非常有用的。

#include<iostream>
using namespace std;
 
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);
  cout << endl;
 
  // 一个表达式中,连续构造+拷贝构造->优化为一个构造
  f1(A(2));
  cout << endl;
 
  // 一个表达式中,连续拷贝构造+拷贝构造->优化一个拷贝构造
  A aa2 = f2();
  cout << endl;
 
  // 一个表达式中,连续拷贝构造+赋值重载->无法优化
  aa1 = f2();
  cout << endl;
  return 0;
}

拷贝对象时的一些编译器优化就提醒我们能在一行写的就在一行写,

尽量往编译器的优化方面靠拢。关于匿名对象和这方面的题就放在下一篇了。

本篇完。

目录
相关文章
|
19小时前
|
设计模式 编译器 C++
【C++航海王:追寻罗杰的编程之路】特殊类的设计方式你知道哪些?
【C++航海王:追寻罗杰的编程之路】特殊类的设计方式你知道哪些?
4 0
|
20小时前
|
存储 编译器 C语言
【C++航海王:追寻罗杰的编程之路】string类
【C++航海王:追寻罗杰的编程之路】string类
5 0
|
20小时前
|
编译器 C++
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(下)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(下)
4 0
|
20小时前
|
存储 编译器 C++
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(中)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(中)
4 0
|
1天前
|
安全 编译器 C++
【C++】学习笔记——类和对象_5
【C++】学习笔记——类和对象_5
17 9
|
1天前
|
编译器 C++
【C++】学习笔记——类和对象_4
【C++】学习笔记——类和对象_4
13 6
|
1天前
|
存储 编译器 C++
【C++】学习笔记——类和对象_3
【C++】学习笔记——类和对象_3
14 6
|
1天前
|
存储 编译器 C语言
【C++】学习笔记——类和对象_2
【C++】学习笔记——类和对象_2
14 3
|
1天前
|
编译器 C语言 C++
【C++】学习笔记——类和对象_1
【C++】学习笔记——类和对象_1
10 3
|
20小时前
|
存储 编译器 C语言
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(上)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(上)
6 2