东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1135|回复: 1
打印 上一主题 下一主题

[C/C++] 深入学习c++(类的c++11后三个基本原则变成了五个基本原则)

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14435
QQ
跳转到指定楼层
楼主
发表于 2021-9-2 09:00:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//深入学习c++(类的c++11后三个基本原则变成了五个基本原则)
//c++11中可以严格区分左值与右值!
//对于一个带有资源的类 深拷贝的  
//原则:1、构造new析构delete 2、拷贝构造函数 3、operator=重载  
//     4、[针对右值]拷贝构造函数 5、[针对右值]operator=重载

C++中的类 :绝对不要在析构函数里面抛出异常
所以:默认的析构函数都添加了关键字 noexcept
原因:
1、C++无法同时捕获多个异常 一个try{}catch{}只能一个异常
   A、多个实例 或 B、多个基类的析构抛异常
2、导致类的有些资源无法释放,内存泄露


c++(类 构造函数失败应该抛出异常)






  1. #include <iostream>
  2. #include <vector>
  3. #include <string.h>
  4. #include <cassert>

  5. using namespace std;

  6. class Teacher
  7. {
  8. private:
  9.     int m_age;
  10.     char* m_name;  //字符指针 资源
  11. public:
  12.     Teacher(int age, char* pname){
  13.         m_age = age;
  14.         m_name = new char[strlen(pname) + 1];
  15.         strcpy(m_name, pname);
  16.     }
  17.     void print_info() const{
  18.         assert(m_name);
  19.         printf("老师的年龄:%d,名字:%s\n", m_age, m_name);
  20.     }
  21.     Teacher(const Teacher& other){
  22.         //拷贝构造函数 Teacher& other 不带const就只能是左值的拷贝构造了
  23.         printf("调用拷贝构造函数,不带const就只能是左值的!\n");
  24.         m_age = other.m_age;
  25.         m_name = new char[strlen(other.m_name) + 1];
  26.         strcpy(m_name, other.m_name);

  27.     }
  28.     Teacher(Teacher&& other){
  29.         //【右值的】拷贝构造函数
  30.         printf("调用【右值的】拷贝构造函数!\n");
  31.         // 右值一般都是临时变量 操作完后就不用了的 利用右值减少拷贝
  32.         m_age = other.m_age;
  33.         m_name = other.m_name;
  34.         other.m_name = nullptr;
  35.         other.m_age = -1;

  36.     }
  37.     Teacher& operator=(const Teacher& other){
  38.         printf("调用operator=重载,不带const就只能是左值的!\n");
  39.         m_age = other.m_age;
  40.         delete [] m_name;   //先释放左边的资源
  41.         m_name = new char[strlen(other.m_name) + 1];
  42.         strcpy(m_name, other.m_name);
  43.         return *this;
  44.     }

  45.     Teacher& operator=(Teacher&& other){
  46.         //Teacher&&代表是右值的引用
  47.         printf("调用【右值的】operator=重载!\n");
  48.         if(&other == this){
  49.             // t1 = std::move(t1);这种情况下
  50.             return *this;
  51.         }
  52.         
  53.         delete [] m_name;  //记得先释放左边的资源
  54.         // m_name = new char[strlen(other.m_name) + 1];
  55.         // strcpy(m_name, other.m_name);
  56.         m_name = other.m_name;
  57.         m_age = other.m_age;
  58.         other.m_name = nullptr;
  59.         other.m_age = -1;
  60.         return *this;
  61.     }

  62.     ~Teacher(){
  63.         printf("调用老师%s析构函数!\n", m_name);
  64.         delete [] m_name;
  65.         m_age = -1;
  66.     }
  67. };

  68. //深入学习c++(类的c++11后三个基本原则变成了五个基本原则)
  69. //c++11中可以严格区分左值与右值!
  70. //对于一个带有资源的类 深拷贝的  
  71. //原则:1、构造new析构delete 2、拷贝构造函数 3、operator=重载  
  72. //     4、[针对右值]拷贝构造函数 5、[针对右值]operator=重载


  73. int main(){
  74.     Teacher t1(30, "jingjinju");
  75.     Teacher t2(33, "izengzhi");
  76.     // c++11中严格区分了 左值与右值   能取地址的就是左值
  77.     //std::move(t1) 就是将左值转为右值  
  78.     // 编译器有能力区分 左值还是右值的 拷贝构造函数  operator=
  79.     Teacher t3 = std::move(t1);
  80.     //特别注意:std::move()调用之后 t1的m_name变null了 t1的资源已经被抽干了
  81.     // t1.print_info();
  82.     t1 = std::move(t2);
  83.     return 0;
  84. }


复制代码



让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

98

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
沙发
发表于 2021-11-23 19:29:48 | 只看该作者
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-27 04:37 , Processed in 0.176122 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表