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

C++中的友元及运算符重载

友元

  • 意义
    • 程序中,有些私有属性也想让类外特殊的一些函数或者类进行访问,就要用到友元技术
  • 关键字
    • friend
  • 友元的三种实现
    • 全局函数做友元
    class Room{friend void test(Person &p);//friend class test;public:string phone_number;private:string bedroom;
    }void test(Person &p){cout << p->bedroom << endl;}
    
    • 类做友元
      • friend class test;
    • 成员函数做友元
      • friend void GoodGay::visit();
      • GoodGay类下的visit成员函数作为本类的好朋友可以访问私有成员

运算符重载

  • 概念
    • 对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
    • 主要是重载函数的函数名就是operator,这是编译器起好的
  • 加号
    • 成员函数重载
    • 调用的本质Person p3 = p1.operator+(p2);
      class Person{public:Person operator+(Person &p){Person temp;temp.m_A = this->m_A + p.m_A;temp.m_B = this->m_B + p.m_Areturn temp;}}
    
    • 全局函数重载
    • 调用的本质Person p3 = operator+(p1, p2);
      Person operator+(Person &p1, Person &p2){Person temp;temp.m_A = p1.m_A + p2.m_A;temp.m_B = p1.m_B + p2.m_B;return temp;}
    
    • 函数重载依然适用于符号重载
  • 左移—输出—可以输出自定义数据类型—需要配合友元
    • 成员函数—不可行—因为无法实现对象在右边这种情况
      • p.operator<<(cout)简化为p << cout
    • 只能利用全局函数重载
      • operator<<(cout, p) 简化 cout << p
      ostream& operator<<(ostream& cout, Person &p){cout << "m_A = " << p.m_A;return cout;}//cout其实也是一个对象,为了满足链式编程,故而不可以重新创建后返回因此采用引用
    
    • 问题—如果是私有属性,这种全局函数无法访问怎么办—友元函数
  • 递增/递减运算符重载—存在前置递增和后置递增
    • 前置递增和后置递增的区分—采用占位参数
    • 前置递增返回的是引用,后置递增返回的是数值
      • 因为后置递增返回的是临时对象
      class Num{friend ostream& operator<<(ostream& cout, Num n);private:int n_Num;public:Num(int a): n_Num(a) {return ;}Num& operator++(void){n_Num++;return *this;}Num operator++(int){Num temp = (n_Num);n_Num++;return temp;}   };ostream& operator<<(ostream& cout, Num& n){cout << n.n_Num << endl;return cout;}int main(){Num n(1);cout << n++ <<endl;cout << n << endl;cout << ++n << endl;return 0;}
    
  • 赋值运算符
    • 赋值运算=其实也是默认就有的—属性值拷贝
    • 编译器提供的赋值运算符的操作是一个浅拷贝的操作,因此在涉及到内存问题时候需要进行深拷贝,故而需要进行重载
    • = 有连续赋值行,因此,涉及到链式编程
      class Age{friend ostream& operator<<(ostream& cout, Age& n);private:int* m_age;public:Age(int age){m_age = new int(age);}Age(const Age& age){if(m_age != NULL){delete m_age;m_age = NULL;}m_age = new int(*age.m_age);}Age& operator=(Age &p){if(m_age != NULL){delete m_age;m_age = NULL;}m_age = new int(*p.m_age);return *this;}~Age(){if(m_age != NULL){delete m_age;m_age = NULL;}}};ostream& operator<<(ostream& cout, Age& n){cout << *n.m_age << endl;return cout;}int main(){Age a(10);Age b(20);Age c(30);cout << a << endl;cout << b << endl;cout << c << endl;a = b = c;cout << a << endl;return 0;}
    
  • 关系运算符
    • 意义
      • 让两个对象可以进行比较
      class Person{public:int m_age;string m_name;Person(string m_name, int m_age){this->m_name = m_name;this->m_age = m_age;}bool operator==(Person& p){if(this->m_age == p.m_age && this->m_name == p.m_name){return true;}return false;}bool operator!=(Person& p){if(this->m_age != p.m_age || this->m_name == p.m_name){return true;}return false;}};
    
  • 函数调用运算
    • ()也可以重载
    • 由于重载后使用的方式非常像函数的调用,因此称为访函数
    • 访函数没有固定写法,非常灵活
      class Myprintf{public:void operator()(string test){cout << test << endl;}}Myprintf printf;printf("hello cpp");
    
  • 匿名函数
    • 类型()
    • Myprintf()(“hello cpp”);
http://www.lryc.cn/news/18515.html

相关文章:

  • 五、运行时数据区内部结构、JVM中的线程
  • Codeforces Round #848 (Div. 2)A-C
  • 机器学习笔记之近似推断(一)从深度学习角度认识推断
  • 指针的进阶
  • 一元二次方程方程的类
  • Ask林曦|来回答,30个你关心的日常问题(二)
  • 哪款电容笔适合开学季?电容笔和Apple Pencil的区别
  • Qt之Qprocess
  • 为什么不愿意专升本 学历有什么用
  • 构造函数的使用大全
  • ASP.NET Core MVC 项目 IOC容器
  • ARM工控机/网关- 钡铼技术
  • 为什么都在喊数据可视化?它究竟怎么做?
  • nodejs+vue停车场停车位短租系统vscode
  • 物理真机上LUKS结合TPM的测试 —— 使用随机数密钥
  • Linux USB 开发指南
  • FreeRTOS入门(03):队列、信号量、互斥量
  • Biome-BGC在模拟过程中,如何使用Linux、Python等,完成前处理和后处理工作???
  • 【unittest学习】unittest框架主要功能
  • 京东测开岗3+1面经+经验分享,拿到offer,月薪34k....
  • 后端接收格式为x-www-form-urlencoded的数据
  • LeetCode 707. 设计链表
  • HTTP的主要作用是什么
  • SpringBoot系列-- @Enable 模块驱动
  • PHP程序员适合创业吗?
  • 2023年CDGA考试-第12章-元数据(含答案)
  • 数据结构之顺序表篇
  • ZBC通证月内已翻倍,Nautilus Chain 上线前夕的“开门红”
  • 人工智能练习题:激活函数需要满足的条件、提高CNN的泛化能力、CNN输出特征图大小计算
  • KingbaseES Json 系列三:Json数据操作函数一