operator的两种用法(重载和隐式类型转换)

重载

隐式类型转换

构造函数的隐式类型转换

利用operator进行的隐式类型转换成为operator算子的隐式类型转换,讲这个之前先了解构造函数的隐式类型转换,请看以下代码

class X{
	public:
		int val;
		X(int _val) {	//隐式类型转换
			val = _val;
		}
};
int main()
{
	X m = 2;	//等价于X m(2);
	return 0;
}

传入一个参数初始化类的构造函数就是构造函数的隐式类型转换,可以理解为将int类型转换为X(class)类型

补充

如果不想出现这种隐式类型转换,就可以用explict修饰
具体详解请看:explict详解

operator算子的隐式类型转换

而operator算子的隐式类型转换则是相反的,例如以下代码

#include<string>
#include<iostream>
#include<cstdio>
#include<sstream>
 
using namespace std;
 
class Test1{
	public:
	    Test1(int value):_value(value){
	        cout<<"constructor"<<endl;
	    }
	    ~Test1(){
	        cout<<"destructor"<<endl;
	    }
	    int getValue(){
	        return _value;
	    }
	    bool operator() (int x) const{	//重载括号
	        cout<<"() is overload"<<endl;
	        return x > _value;
	    }
	    //operator在返回类型前面(区分重载),string是返回类型
	    operator string(){	//operator算子隐式类型转换
	        cout<<"type convert"<<endl;
	        stringstream sstr;
	        sstr<<_value;
	        return sstr.str();
		}
	private:
	    int _value;
};
 
int main(){
    Test1 t(10);
    int i = 5;
    if(t(5))
        cout<<i<<" is greater than "<<t.getValue()<<endl;
    else
        cout<<i<<" is less than "<<t.getValue()<<endl;

    string str(t); // 将Test1类型转换为string类型
    cout<<str<<endl;
    return 0;
}

Test1类型的对象传入string的构造函数,是用了c++构造函数的隐式类型转换特性,虽然string类并没有显式定义参数为Test1的构造函数,但因为其可以隐式转换为string,所以语法上都是合法的。

构造函数的隐式类型转换,是使用一个其他的类型构造当前类的临时对象并用此临时对象来构造当前对象,这种转换必须有构造函数的支持;

operator算子的隐式类型转换,使用当前对象去生成另一个类型的对象(正好与构造函数隐式转换相反),这种转换必须有operator算子的支持。

当然了,构造函数的隐式类型转换有利有弊,类的设计者就起决定性作用了,如果你不想让构造函数发生隐式的类型转换,请在构造函数前加explicit关键字;同时,operator算子声明的隐式类型转换也可以通过一些相应的返回值函数替代,用户的掌控性更好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的Doraemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值