C++第七弹---类与对象(四)

简介: C++第七弹---类与对象(四)



1、拷贝构造函数

1.1、概念

在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。


那在创建对象时,可否创建一个与已存在对象一某一样的新对象呢?

拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。

1.2、特征

拷贝构造函数也是特殊的成员函数,其特征如下:

1. 拷贝构造函数是构造函数的一个重载形式。
2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。

#include<iostream>
using namespace std;
class Date
{
public:
  Date(int year = 1900, int month = 1, int day = 1)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  // Date(const Date& d) // 正确写法
  //C++规定自定义的类型传值调用时都会调用拷贝构造
  Date(const Date d) // 错误写法:编译报错,会引发无穷递归
  {
    _year = d._year;
    _month = d._month;
    _day = d._day;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d1;
  Date d2(d1);
  return 0;
}

3. 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按
字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝

内置类型编译器默认生成的拷贝构造函数---浅拷贝

class Date
{
public:
  Date(int year,int month,int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d1(2022, 2, 22);
  Date d2(d1);
  return 0;
}

调试结果如下图:

通过调试可以知道,我们不手动写构造函数,编译器会自动生成默认构造函数。

自定义类型

#include<iostream>
using namespace std;
class Time
{
public:
  Time()
  {
    _hour = 1;
    _minute = 1;
    _second = 1;
  }
  Time(const Time& t)
  {
    _hour = t._hour;
    _minute = t._minute;
    _second = t._second;
    cout << "Time::Time(const Time&)" << endl;
  }
private:
  int _hour;
  int _minute;
  int _second;
};
class Date
{
private:
  // 基本类型(内置类型)
  int _year = 1970;
  int _month = 1;
  int _day = 1;
  // 自定义类型
  Time _t;
};
int main()
{
  Date d1;
  // 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
  // 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
  Date d2(d1);
  return 0;
}

注意:

在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
义类型是调用其拷贝构造函数完成拷贝的。

4. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?
当然像日期类这样的类是没必要的。那么下面的类呢?验证一下试试?

// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
#include<iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
  Stack(size_t capacity = 10)
  {
    _array = (DataType*)malloc(capacity * sizeof(DataType));
    if (nullptr == _array)
    {
      perror("malloc申请空间失败");
      return;
    }
    _size = 0;
    _capacity = capacity;
  }
  void Push(const DataType& data)
  {
    // CheckCapacity();
    _array[_size] = data;
    _size++;
  }
  ~Stack()
  {
    if (_array)
    {
      free(_array);
      _array = nullptr;
      _capacity = 0;
      _size = 0;
    }
  }
private:
  DataType* _array;
  size_t _size;
  size_t _capacity;
};
int main()
{
  Stack s1;
  s1.Push(1);
  s1.Push(2);
  s1.Push(3);
  s1.Push(4);
  Stack s2(s1);
  return 0;
}

注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
时,则拷贝构造函数是一定要写的,否则就是浅拷贝。


5. 拷贝构造函数典型调用场景:

使用已存在对象创建新对象。
函数参数类型为类类型对象。
函数返回值类型为类类型对象。

#include<iostream>
using namespace std;
class Date
{
public:
  Date(int year, int minute, int day)
  {
    cout << "Date(int,int,int):" << this << endl;
  }
  Date(const Date& d)
  {
    cout << "Date(const Date& d):" << this << endl;
  }
  ~Date()
  {
    cout << "~Date():" << this << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};
Date Test(Date d)
{
  Date temp(d);
  return temp;
}
int main()
{
  Date d1(2022, 1, 13);
  Test(d1);
  return 0;
}

注意:此处因为编译器优化的原因只有一次构造函数,两次拷贝构造(传值调用的拷贝构造被优化了)。

为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用
尽量使用引用。

2、运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其
返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似

函数名字为:关键字operator后面接需要重载的运算符符号。

函数原型:返回值类型 operator操作符(参数列表)

注意:

1、不能通过连接其他符号来创建新的操作符:比如operator@。

2、重载操作符必须有一个类类型参数

3、用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义

作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐

藏的this

4、.*   ::   sizeof  ?:   .  注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

2.1、等号运算符重载

使用公有权限和友元实现全局等于运算符重载

//全局operator==
class Date
{
public:
  Date(int year = 2022,int month = 2 ,int day = 22)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  friend bool operator==(const Date& d1, const Date& d2);
private:
  int _year;
  int _month;
  int _day;
};
// 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
// 方案1、使用public(公有)访问限定符
//bool operator==(const Date& d1, const Date& d2)
//{
//  return d1._year == d2._year
//    && d1._month == d2._month
//    && d1._day == d2._day;
//}
//方案2、使用友元,即在类中对函数进行声明,并在函数前面加friend关键字
bool operator==(const Date& d1, const Date& d2)
{
  return d1._year == d2._year
    && d1._month == d2._month
    && d1._day == d2._day;
}
int main()
{
  Date d1(2022, 1, 1);
  Date d2(2022, 1, 2);
  if (d1 == d2)
  {
    cout << "d1==d2" << endl;
  }
  else
  {
    cout << "d1!=d2" << endl;
  }
  return 0;
}

使用类成员函数实现等于操作符重载

class Date
{
public:
  Date(int year, int month, int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  // bool operator==(const Date* this, const Date& d2)
    // 这里需要注意的是,左操作数是this,指向调用函数的对象
  // _year 是实质是this->_year
  bool operator==(const Date& d)
  {
    return _year == d._year
      && _month == d._month
      && _day == d._day;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d1(2022, 1, 1);
  Date d2(2022, 1, 2);
  if (d1 == d2)
  {
    cout << "d1==d2" << endl;
  }
  else
  {
    cout << "d1!=d2" << endl;
  }
  return 0;
}

总结

本篇博客就结束啦,谢谢大家的观看,如果公主少年们有好的建议可以留言喔,谢谢大家啦!

相关文章
|
6天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
13 0
|
5天前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
4天前
|
编译器 C++
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
5天前
|
编译器 C++
【C++】类和对象(中)
【C++】类和对象(中)
|
5天前
|
存储 编译器 程序员
【C++】类和对象(上)
【C++】类和对象(上)
|
6天前
|
存储 编译器 C++
【C++】类和对象(下)
【C++】类和对象(下)
|
6天前
|
存储 算法 搜索推荐
【C++】类的默认成员函数
【C++】类的默认成员函数
|
11天前
|
C++
C++ --> 类和对象(三)
C++ --> 类和对象(三)
26 9