C++中的operator操作符的用法:重载和隐式类型转换

来源:互联网 发布:gst5000软件 编辑:程序博客网 时间:2024/05/15 23:45

作者:zero_learner

地址:http://www.cnblogs.com/yangxudong/p/3872053.html


C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换。对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式转换,我们下面就用以下这个小例子温故一下这两种用法:

#include <iostream>#include <sstream>                                                                                                                                                     using namespace std;class FuncObj{public:  FuncObj(int n): _n(n) {    cout << "constructor" << endl;  }    bool operator()(int v) {    cout << "operator overload" << endl;    return v > _n;   }  operator string() {    cout << "type convert" << endl;    stringstream sstr;    sstr << _n;     return sstr.str();  }  int _n; };int main(){  FuncObj obj(10);  if (obj(11))    cout << "11 greater than 10" << endl;  string str(obj);  cout << str << endl;}


第12行是操作符重载,重载()使得该对象成为一个函数对象,即该对象有类似函数的功能,在很多场合下可以当成函数指针使用,在STL的很多算法模板里广泛使用。FuncObj用过操作符重载可以判断传入的参数是否大于一个预先设定好的值(在构造函数里指定),见代码的29~31行。

17行的定义表名FuncObj对象可以隐身转换成string,这就是operator的第二个用法,使用方法见代码的33~34行。注意在函数声明时,operator关键词出现在返回类型的前面,区别与操作符重载时的用法。

上述代码的输出:

constructoroperator overload11 greater than 10type convert10

顺便说点题外话,第33行把FuncObj类型的对象传入string的构造函数,是用了c++构造函数的隐式类型转换特性,虽然string类并没有显式定义参数为FuncObj的构造函数,但因为其可以隐式转换为string,所以语法上都是合法的。构造函数的隐式类型转换,是使用一个其他的类型构造当前类的临时对象并用此临时对象来构造当前对象,这种转换必须有构造函数的支持;operator算子的隐式类型转换,使用当前对象去生成另一个类型的对象(正好与构造函数隐式转换相反),这种转换必须有operator算子的支持。当然了,构造函数的隐式类型转换有利有弊,类的设计者就起决定性作用了,如果你不想让构造函数发生隐式的类型转换,请在构造函数前加explicit关键字;同时,operator算子声明的隐式类型转换也可以通过一些相应的返回值函数替代,用户的掌控性更好。

最后,用过实现一个经常发生的普遍需求(string转其他基本数据类型)让读者加深一下,operator自定义对象类型的隐式转换功能的用法。

template <typename T>  class string_cast  {  public:    string_cast(const std::string &from): m_from(from) {    }    operator T() const {      std::stringstream sstr(m_from);      T ret;      try {        sstr >> ret;      }      catch(std::exception &e)      {         return T(0);      }      return ret;    }  private:    const std::string &m_from;  };

string转int的用法:
cout << string_cast<int>("12345") << endl;
string转double的用法:
cout << string_cast<double>("12345.78") << endl;



阅读全文
0 0
原创粉丝点击