当前位置: 首页 > news >正文

又拍网站怎么做楚雄今日头条新闻

又拍网站怎么做,楚雄今日头条新闻,找私人做网站程序费用,建什么网站好文章目录 前言一、拷贝构造函数1. 概念2. 特征3. 编译器生成默认拷贝构造函数4. 拷贝构造函数典型使用场景 二、运算符重载函数三、赋值运算符重载函数1. 赋值运算符重载格式2. 赋值运算符只能重载成类的成员函数不能重载成全局函数3.编译器生成一个默认赋值运算符重载4. 运算符…

文章目录

  • 前言
  • 一、拷贝构造函数
    • 1. 概念
    • 2. 特征
    • 3. 编译器生成默认拷贝构造函数
    • 4. 拷贝构造函数典型使用场景
  • 二、运算符重载函数
  • 三、赋值运算符重载函数
    • 1. 赋值运算符重载格式
    • 2. 赋值运算符只能重载成类的成员函数不能重载成全局函数
    • 3.编译器生成一个默认赋值运算符重载
    • 4. 运算符重载函数和赋值运算符重载函数的调用
  • 四、前置++和后置++重载
  • 总结


前言

C++拷贝构造函数、运算符重载函数、赋值运算符重载函数、前置++和后置++重载等的介绍


一、拷贝构造函数

在实际运用中,我们有时候需要操作一个实例化好的类,但是又不想改变这个类,这时候,我们就需要拷贝构造函数。

1. 概念

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

2. 特征

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

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

拷贝构造函数是构造函数的一个重载形式,所以拷贝构造函数的函数名与类名相同,参数与构造函数不同,无返回值。

如下图:

  • 采用传值调用,会引发无穷递归,编译器强制检查报错。
    在这里插入图片描述

  • 应该采用类类型对象的引用,如下:

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1945, int month = 8, int day = 15){_year = year;_month = month;_day = day;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}~Date(){_year = 0;_month = 0;_day = 0;}
private:int _year;int _month;int _day;
};int main()
{Date d1(2024, 6, 22);Date d2(d1);return 0;
}

在这里插入图片描述

3. 编译器生成默认拷贝构造函数

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

简单来说就是,不显示定义,编译器默认生成一个构造函数

  • 对于内置类型会进行值拷贝(浅拷贝)。
  • 对于自定义类型会调用它的拷贝构造函数。
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1945, int month = 8, int day = 15){_year = year;_month = month;_day = day;}~Date(){_year = 0;_month = 0;_day = 0;}
private:int _year;int _month;int _day;
};int main()
{Date d1(2023, 9, 1);Date d2(d1);return 0;
}

在这里插入图片描述

编译器默认生成的拷贝构造函数是值拷贝(浅拷贝),但是有些情况下我们需要深拷贝。比如:
对一个栈,我们不能直接浅拷贝,程序会崩溃。原因如下:

浅拷贝会将一个栈动态申请的空间的地址拷贝个另一个栈的指针,所以两个栈对象的指针指向同一块空间,析构函数会对同一块空间释放两次,程序会崩溃。

#include <iostream>
using namespace std;class Stack
{
public:Stack(int capacity = 4){_a = (int*)malloc(sizeof(int) * capacity);if (nullptr == _a){perror("Stack malloc");return;}_top = 0;_capacity = capacity;}void Push(int x){// 增容_a[_top] = x;_top++;}~Stack(){if (_top){free(_a);_a = nullptr;_top = 0;_capacity = 0;}}
private:int* _a;int _top;int _capacity;
};int main()
{Stack st1(10);st1.Push(1);st1.Push(2);st1.Push(3);st1.Push(4);Stack st2(st1);return 0;
}

在这里插入图片描述


在这里插入图片描述

  • 所以,这种Stack的类对象,我们应该自己显示定义拷贝构造函数。
#include <iostream>
using namespace std;class Stack
{
public:Stack(int capacity = 4){_a = (int*)malloc(sizeof(int) * capacity);if (nullptr == _a){perror("Stack malloc");return;}_top = 0;_capacity = capacity;}Stack(const Stack& st){_a = (int*)malloc(sizeof(int) * st._capacity);if (nullptr == _a){perror("Stack malloc");return;}memcpy(_a, st._a, sizeof(int) * st._top);_top = st._top;_capacity = st._capacity;}void Push(int x){// 增容_a[_top] = x;_top++;}~Stack(){if (_top){free(_a);_a = nullptr;_top = 0;_capacity = 0;}}
private:int* _a;int _top;int _capacity;
};int main()
{Stack st1(10);st1.Push(1);st1.Push(2);st1.Push(3);st1.Push(4);Stack st2(st1);return 0;
}

在这里插入图片描述

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

4. 拷贝构造函数典型使用场景

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

二、运算符重载函数

在介绍运算符重载之前先来看看运算符重载和函数重载的比较

  • 函数重载和运算符重载都用了“重载”一词,但是两者重载没有关系
  • 函数重载是为了函数名相同,函数参数不同的函数可以同时存在
  • 运算符重载是为了自定义类型的数据也可以像内置类型一样使用运算符进行比较或赋值等

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。函数名字为:关键字operator后面接需要重载的运算符符号
函数原型:返回值类型 operator操作符(参数列表)

注意:

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型参数
  • 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  • .*、 ::、 sizeof 、?: . 注意以上5个运算符不能重载**
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1945, int month = 8, int day = 15){_year = year;_month = month;_day = day;}~Date(){_year = 0;_month = 0;_day = 0;}
//private:int _year;int _month;int _day;
};bool operator<(const Date& x1, const Date& x2)
{if (x1._year < x2._year){return true;}else if (x1._year == x2._year && x1._month < x2._month){return true;}else if (x1._year == x2._year && x1._month == x2._month && x1._day < x2._day){return true;}else{return false;}
}bool operator>(const Date& x1, const Date& x2)
{if (x1._year > x2._year){return true;}else if (x1._year == x2._year && x1._month > x2._month){return true;}else if (x1._year == x2._year && x1._month == x2._month && x1._day > x2._day){return true;}else{return false;}
}int main()
{Date d1(1945, 8, 15);Date d2(1949, 10, 1);cout << operator<(d1, d2) << endl;cout << (d1 < d2) << endl;cout << operator>(d1, d2) << endl;cout << (d1 > d2) << endl;return 0;
}

在这里插入图片描述

使用运算符重载时,调用运算符重载函数,也可以直接使用运算符比较。

	cout << operator<(d1, d2) << endl;cout << (d1 < d2) << endl;cout << operator>(d1, d2) << endl;cout << (d1 > d2) << endl;
  • 一般建议直接使用运算符比较。

上述运算符重载到了全局中,运算符重载也可以重载到类中。

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1945, int month = 8, int day = 15){_year = year;_month = month;_day = day;}bool operator<(const Date& x2){if (_year < x2._year){return true;}else if (_year == x2._year && _month < x2._month){return true;}else if (_year == x2._year && _month == x2._month && _day < x2._day){return true;}else{return false;}}~Date(){_year = 0;_month = 0;_day = 0;}
private:int _year;int _month;int _day;
};int main()
{Date d1(1945, 8, 15);Date d2(1949, 10, 1);cout << d1.operator<(d2) << endl;cout << (d1 < d2) << endl;return 0;
}

在这里插入图片描述

三、赋值运算符重载函数

1. 赋值运算符重载格式

  • 参数类型:const T&,传递引用可以提高传参效率
  • 返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
  • 检测是否自己给自己赋值
  • 返回*this :要复合连续赋值的含义
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1945, int month = 8, int day = 15){_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;}~Date(){_year = 0;_month = 0;_day = 0;}
private:int _year;int _month;int _day;
};int main()
{Date d1(1945, 8, 15);Date d2(1949, 10, 1);//d2.operator=(d1);d2 = d1;return 0;
}

在这里插入图片描述

赋值运算符可以选择调用赋值运算符重载函数,也可以直接使用赋值运算符,一般建议直接使用赋值运算符。

	d2.operator=(d1);d2 = d1;

2. 赋值运算符只能重载成类的成员函数不能重载成全局函数

原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。

class Date
{
public:Date(int year = 1945, int month = 8, int day = 15){_year = year;_month = month;_day = day;}~Date(){_year = 0;_month = 0;_day = 0;}
//private:int _year;int _month;int _day;
};Date& operator=(Date& left, const Date& right)
{left._year = right._year;left._month = right._month;left._day = right._day;return left;
}

在这里插入图片描述

3.编译器生成一个默认赋值运算符重载

用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

注意:
内置类型成员变量是直接赋值的,
而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。

注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必须要实现。

4. 运算符重载函数和赋值运算符重载函数的调用

  • 内置类型使用运算符进行操作直接转换成指令进行操作。
  • 自定义类型若直接使用运算符操作,会调用对应的运算符重载函数。

四、前置++和后置++重载

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1328, int month = 1, int day = 4){_year = year;_month = month;_day = day;}// 前置++Date& operator++(){_day += 1;return *this;}// 后置++Date operator++(int){Date tmp(*this);_day += 1;return tmp;}
private:int _year;int _month;int _day;
};int main()
{Date d;Date d1(1949, 10, 1);d = d1++; // d: 1949 10 1   d1: 1949 10 2d = ++d1; // d: 1949 10 3   d1: 1949 10 3return 0;
}
  • 前置++
    前置++:返回+1之后的结果
    注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
  • 后置++
    前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
    C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
    注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,然后给this+1而temp是临时对象,因此只能以值的方式返回,不能返回引用

总结

C++拷贝构造函数、运算符重载函数、赋值运算符重载函数、前置++和后置++重载等的介绍。

http://www.shuangfujiaoyu.com/news/61819.html

相关文章:

  • 天津网站在哪里建设付费推广
  • 广州企业网站模板建站软文代发平台
  • 网站建设人员分工表北京专业seo公司
  • 网站开发需要掌握的知识怎样做网站卖自己的产品
  • 如何个网站做二维码企业建站公司
  • 做公司网站的好处重庆网站到首页排名
  • 大良网站智能推广价格广告优化师工作内容
  • tag 网站备案线下引流推广方法
  • 做网站要学什么软件好广告开户
  • 星巴克网站建设seo服务商排名
  • 自我建设外贸网站海外seo网站推广
  • 建站公司排名 软通如何查看百度搜索指数
  • 网站开发需求说明书模板短视频运营是做什么的
  • org后缀做网站行网页设计制作网站
  • html欧美网站模板汕头网站建设方案推广
  • 公司网站手机版模板绍兴seo排名外包
  • 黄岛开发区做网站的公司百度seo提高排名费用
  • DW怎么做网站下拉菜单网站关键词seo费用
  • paypal网站集成网站推广培训
  • 做网站策划遇到的问题太原搜索排名提升
  • 大连网站建设好的公司外贸独立站推广
  • 萧山好的做网站的公司什么是电商?电商怎么做
  • 怎么判断网站优化过度网络营销推广专家
  • 一个企业该如何进行网络营销seo顾问赚钱吗
  • 中职学校网站建设的厂家无货源电商怎么做
  • 在线网站制作平台seo短期课程
  • 互联网推广销售是做什么的兰州seo优化公司
  • 网站做链接的意义是什么营销型网站建设步骤
  • 网站建设功能模块图沈阳专业网站seo推广
  • 网站建设价格费用博为峰软件测试培训学费