33,311
社区成员
发帖
与我相关
我的任务
分享
class String
{
public:
char *m_s;
char& operator[](int i);
friend bool operator==(const String& s1,const String &s2);//没有私有成员,friend多余?
};
char& String::operator[](int i)
{
return m_s[i];
}
bool operator==(const String& s1,const String &s2)
{
for(int i=0;s1[i];i++)//error:没有与这些操作数匹配的"[]"运算符
{
if(s1.m_s[i]!=s2.m_s[i])
return FALSE;
}
return TRUE;
}
还剩这个问题:
"==" 可能是bool operator==(const String &,const String &)
也可能是内置C++ operator==(char *, char *)”
当调用形如(String, CHAR [260])
貌似调用形如(String, CHAR [260])时类经过opertor char*转换 与c语言指针地址判等的那种用法产生了二义性
如何避免,还是想不出
for(int i=0;s1[i];i++)//OK
for(int i=0;s1[i]=3;i++)//error
如此测试结果是否说明s1[]只要出现在条件里总是调用只读版的,那个可写的版本何时才会被编译器调用?