• 【C++入门】不能重载为友元函数的4个运算符(=, ->, [ ], ( ))


    1、什么是友元函数

    参考博客:《【C++入门】友元函数详解(定义、实现、优缺点)》

    2、什么是运算符重载

    参考博客:《【C++入门】运算符重载详解》

    3、赋值运算符的示例代码

    3.1、不重载赋值运算符

    3.1.1、示例代码

    #include 
    
    using namespace std;
    
    
    class Person
    {
    private:
    	int age;
    
    public:
    	//构造函数
    	Person(){};
    	Person(int a);
    	
    };
    
    Person::Person(int a)
    {
    	cout << "Person(int a)" << endl;
    
    	this->age = a;
    }
    
    int main()
    {
    	Person p;
    	
    	p = 2;	//实际相当于p=Person(2)
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.1.2、代码执行结果分析

    [root#]$ ./app 
    Person(int a)
    
    • 1
    • 2

    (1)可以看到"p=2"实际调用的是构造函数,实际相当于p=Person(2);
    (2)真实的代码执行过程:先调用构造函数"Person(int a)"把2转换成Person类,然后再调用默认的赋值运算符函数,实现赋值运算;

    3.2、用成员函数重载赋值运算符

    3.2.1、示例代码

    #include 
    
    using namespace std;
    
    
    class Person
    {
    private:
    	int age;
    
    public:
    	Person(){};
    	Person(int a);
    
    	//赋值运算符重载函数
    	Person operator=(int x);
    	
    };
    
    Person::Person(int a)
    {
    	cout << "Person(int a)" << endl;
    	this->age = a;
    }
    
    
    Person Person::operator=(int x)
    
    {
    	cout << "operator=" << endl;
    
    	this->age = x;
    
    	return *this;
    }
    
    int main()
    {
    	Person p;
    	
    	p = 2;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    3.2.2代码执行结果分析

    [root#]$ ./app 
    operator=
    
    • 1
    • 2

    当类用成员函数实现了赋值运算符的重载,编译器就会去调用匹配上的赋值运算符重载函数;

    3.3、用友元函数重载赋值运算符

    3.3.1、示例代码

    #include 
    
    using namespace std;
    
    
    class Person
    {
    private:
    	int age;
    
    public:
    	Person(){};
    	Person(int a);
    
    	friend Person operator=(int x);
    	
    };
    
    Person::Person(int a)
    {
    	cout << "Person(int a)" << endl;
    	this->age = a;
    }
    
    
    Person operator=(int x)
    
    {
    	cout << "operator=" << endl;
    
    	this->age = x;
    
    	return *this;
    }
    
    int main()
    {
    	Person p;
    	
    	p = 2;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    3.3.2、代码执行结果分析

    g++ main.cpp -o app --std=c++11
    main.cpp:16:31: error: ‘Person operator=(int)’ must be a nonstatic member function
      friend Person operator=(int x);
                                   ^
    main.cpp:27:23: error: ‘Person operator=(int)’ must be a nonstatic member function
     Person operator=(int x)
                           ^
    make: *** [all] Error 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编译器会报错:赋值运算符必须是非静态成员函数,也就是不能用友元函数来重载赋值运算符;

    4、赋值运算符不能重载为友元函数的原因

    (1)当类没有重载赋值运算符:编译器会调用和赋值运算符右值类型匹配的构造函数,先把右值构造成同样的类,再调用默认的赋值运算符函数;
    (2)当类用成员函数重载赋值运算符:编译器会调用匹配的赋值运算符重载函数;
    (3)当用友元函数重载赋值运算符:编译器首先会去看类是否有重载赋值运算符,虽然已经用友元函数去重载赋值运算符,但是友元函数不是类的成员函数,编译器还是认为类没有重载赋值运算符,去调用匹配的构造函数;但是实际上我们已经用友元函数去重载了赋值运算符并且是能匹配上的,这就产生了歧义,所以编译器会报错;

    5、"=, ->, [], ()"不能用友元函数重载的原因

    (1)当编译器检测到没有重载运算符时就会调用默认的运算符函数,所以我们没有重载这几个运算符也可以正常使用,比如:类的对象指针都是可以用->来操作成员,并不需要我们重载;
    (2)但是当我们用友元函数重载上述运算符时,因为友元函数并不是类成员函数,编译器仍然认为没有重载运算符,会去调用匹配的构造函数再调用默认的赋值运算符函数,但是实际上我们已经用友元函数重载了赋值运算符,这就产生了歧义;
    总结:上述运算符类会提供默认的函数,用友元函数去实现重载会有歧义,并且就算你不小心用友元函数去重载了上述运算符,编译器也会报错;

  • 相关阅读:
    企业级logstash简单使用(ELK)
    Python接口自动化 —— token登录(详解)
    【漏洞分析】KaoyaSwap 安全事件分析
    我变秃了,也变强了——再探博客调优
    通天星CMSV6车载监控平台CompanyList信息泄露漏洞
    【C语言刷LeetCode】1592. 重新排列单词间的空格(E)
    Linux从入门到精通(十)——进程管理
    vue3:arco design message封装
    Stable Diffusion WebUI使用AnimateDiff插件生成动画
    2.26回顾章节主体线索脉络,课程要求(评分)
  • 原文地址:https://blog.csdn.net/weixin_42031299/article/details/127699652