operator char*() const {return ...;}
Effective C++ 2nd 中文版P94
class String{
public:
String(const char * value);
...
operator char*() const {return data;}
private:
char *data;
};
const String s="Hello";
char *nasty=s; //调用char*() const
*nasty='M';
cout<<s;
有两个小疑问:
operator char *()const {return data;}
1.没有写明返回类型
2.具体什么含义
问题点数:150、回复次数:11Top
1 楼icecools(浮生若梦)回复于 2002-10-16 10:35:20 得分 10
返回的是char *
有了这个东西在需要转换到char*的时候会自动调用这个函数,就想隐式转换一样Top
2 楼iicup(双杯献酒)回复于 2002-10-16 10:52:06 得分 10
这是一个只有返回值,没有函数名称的成员函数。
因为它用于类型转换,不会显式用到函数名字。Top
3 楼anyoshon(好学)回复于 2002-10-16 10:53:08 得分 10
operator char *()const {return data;}
这应该是重载了.具体含义,也有点混淆哦.
我个人觉得,operator 的前面应该是char*,表示返回类型.
我去看看上下文,再来交流.Top
4 楼blue_coco(椰子)回复于 2002-10-16 10:55:41 得分 10
operator char *()const {return data;}
我觉得是对()的重载,返回类型用了C/C++的默认类型。
void operator char *()const {return data;}
char *p;
A = p();
Top
5 楼srm2000(华山猛男刀)回复于 2002-10-16 11:17:22 得分 10
operator char *()const {return data;}
返回值为指向字符的指针(char *),即 data指向的首地址
用于类型转换Top
6 楼anrxhzh(百宝箱)回复于 2002-10-16 12:12:41 得分 40
http://www.cs.unc.edu/~smithja/C++Nov97/www.cygnus.com/misc/wp/nov97/special.html
12.3.2 Conversion functions [class.conv.fct]
1 A member function of a class X with a name of the form
conversion-function-id:
operator conversion-type-id
conversion-type-id:
type-specifier-seq conversion-declaratoropt
conversion-declarator:
ptr-operator conversion-declaratoropt
specifies a conversion from X to the type specified by the conversion-
type-id. Such member functions are called conversion functions.
Classes, enumerations, and typedef-names shall not be declared in the
type-specifier-seq. Neither parameter types nor return type can be
specified. The type of a conversion function (_dcl.fct_) is "function
taking no parameter returning conversion-type-id." A conversion func-
tion is never used to convert a (possibly cv-qualified) object to the
(possibly cv-qualified) same object type (or a reference to it), to a
(possibly cv-qualified) base class of that type (or a reference to
it), or to (possibly cv-qualified) void.1)
2 [Example:
class X {
// ...
public:
operator int();
};
void f(X a)
{
int i = int(a);
i = (int)a;
i = a;
}
In all three cases the value assigned will be converted by
X::operator int(). --end example]
_________________________
1) Even though never directly called to perform a conversion, such
conversion functions can be declared and can potentially be reached
through a call to a virtual conversion function in a base class
3 User-defined conversions are not restricted to use in assignments and
initializations. [Example:
void g(X a, X b)
{
int i = (a) ? 1+a : 0;
int j = (a&&b) ? a+b : i;
if (a) { // ...
}
}
--end example]
4 The conversion-type-id shall not represent a function type nor an
array type. The conversion-type-id in a conversion-function-id is the
longest possible sequence of conversion-declarators. [Note: this pre-
vents ambiguities between the declarator operator * and its expression
counterparts. [Example:
&ac.operator int*i; // syntax error:
// parsed as: &(ac.operator int *) i
// not as: &(ac.operator int)*i
The * is the pointer declarator and not the multiplication operator.
] ]
5 Conversion functions are inherited.
6 Conversion functions can be virtual.
Top
7 楼zhanghk(lion)回复于 2002-10-16 12:16:46 得分 10
重载了char*的类型转换,返回类型就是你要转换的类型,即char*Top
8 楼fangrk(加把油,伙计!)回复于 2002-10-16 13:57:33 得分 0
谢谢大家,特别是anrxhzh(百宝箱)。
再讨论讨论,我会增加题目的分数的。Top
9 楼anyoshon(好学)回复于 2002-10-16 15:54:51 得分 30
我总与给你找到答案了.是从<<More Effective C++>>上面找来的.应该是正确的,现在我也明白了这个问题,谢谢.
对不起,我先前是瞎说了:)
隐式类型转换运算符只是一个样子奇怪的成员函数:operator 关键字,其后跟一个类型符号。你不用定义函数的返回类型,因为返回类型就是这个函数的名字。例如为了允许Rational(有理数)类隐式地转换为double类型(在用有理数进行混合类型运算时,可能有用),你可以如此声明Rational类:
class Rational {
public:
...
operator double() const; // 转换Rational类成
}; // double类型
在下面这种情况下,这个函数会被自动调用:
Rational r(1, 2); // r 的值是1/2
double d = 0.5 * r; // 转换 r 到double,
// 然后做乘法
Top
10 楼step_by_step(脚印)回复于 2002-10-16 16:23:35 得分 20
《More effective c++》Item M5中讲的比较清楚。Top
11 楼fangrk(加把油,伙计!)回复于 2002-10-17 17:38:41 得分 0
我在Effective C++中间也看到了More Effective C++ M5有。谢谢大家。结帐!Top
相关问题
- bool operator==(const char* sz); and operator const char* () const; 两句话什么意思?
- const char* const* const* p;
- 三个const的用途各为什么?const char& String::operator[] (const int index) const
- const char *和ansistring
- const char * 如何变成char *?
- const char *iceCream[]与const char iceCream[]不同
- char const *p和char * const p区别?
- 关于运算符重载的问题。(operator char *()const {...})
- static const char*问题
- 请问这几个的区别const char * pstr; char* const pstr;const char* const pstr;




