诡异的操作符重载:
例如下面的这个:
operator const char *
这个操作符重载是以‘const char *’类型返回这个类的值
用法举例:
#include
#include
class AString
{
public:
AString(char* s) { ps = new char[strlen(s) + 1 ]; strcpy(ps, s);}
~AString() {if (ps) delete [] ps;}
operator const char *() {return ps;}
private:
char* ps;
};
int main()
{
AString s("Hello world");
printf("%s\n", (const char*)s);
return 0;
}
阅读(4575) | 评论(0) | 转发(0) |