什么是运算符string(){一些代码}? [英] what does the operator string() { some code } do?

查看:106
本文介绍了什么是运算符string(){一些代码}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类中有以下代码:

operator string() {
        return format("CN(%d)", _fd);
}

想知道这个操作符是什么。

And wanted to know what this operator does.

我熟悉常见的字符串操作符:

I am familiar with the usual string operators:

bool operator==(const string& c1, const string& c2);
bool operator!=(const string& c1, const string& c2);
bool operator<(const string& c1, const string& c2);
bool operator>(const string& c1, const string& c2);
bool operator<=(const string& c1, const string& c2);
bool operator>=(const string& c1, const string& c2);
string operator+(const string& s1, const string& s2 );
string operator+(const Char* s, const string& s2 );
string operator+( Char c, const string& s2 );
string operator+( const string& s1, const Char* s );
string operator+( const string& s1, Char c );
string& operator+=(const string& append);
string& operator+=(const Char* append);
string& operator+=(const Char  append);
ostream& operator<<( ostream& os, const string& s );
istream& operator>>( istream& is, string& s );
string& operator=( const string& s );
string& operator=( const Char* s );
string& operator=( Char ch );
Char& operator[]( size_type index );
const Char& operator[]( size_type index ) const;

...但不是这个?

推荐答案

operator Type() { ... }

是(隐式)转换运算符。例如,如果 Animal 实现运算符string(),则代码

is the (implicit) conversion operator. For example, if class Animal implements operator string(), then the code

Animal a;
...
do_something_with ( (string)a );

会变成类似

do_something_with ( (Animal::operator string)(&a) );

请参阅http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic = / com.ibm.xlcpp8a.doc / language / ref / cplr385.htm 了解更多示例。

这篇关于什么是运算符string(){一些代码}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆