操作符重载==
来源:百度知道 编辑:UC知道 时间:2024/05/24 15:26:47
这是我定义类Str的一部分:
class Str{
bool operator==(const Str& rhs) const
{
if(data == rhs.data) return true;
else return false;
}
bool operator!=(const Str& rhs) const
{
if(data != rhs.data) return true;
return false;
}
private:
std::vector<char> data;
};
实现文件中:
int main()
{
Str s1("quwei");
Str s2("quweI");
if(s1 == s2)
cout << "Y" << endl;
else
cout << "N" << endl;
return 0;
}
运行以后竟然输出Y,
环境: VC++6.0
class Str{
bool operator==(const Str& rhs) const
{
if(data == rhs.data) return true;
else return false;
}
bool operator!=(const Str& rhs) const
{
if(data != rhs.data) return true;
return false;
}
private:
std::vector<char> data;
};
实现文件中:
int main()
{
Str s1("quwei");
Str s2("quweI");
if(s1 == s2)
cout << "Y" << endl;
else
cout << "N" << endl;
return 0;
}
运行以后竟然输出Y,
环境: VC++6.0
#include <iostream>
#include <vector>
using namespace std;
class Str
{
public:
Str(){}
Str(const char * s):data(s,s+strlen(s)){}
bool operator==(const Str& rhs) const
{
if(data == rhs.data) return true;
else return false;
}
bool operator!=(const Str& rhs) const
{
if(data != rhs.data) return true;
return false;
}
private:
std::vector<char> data;
};
int main()
{
Str s1("quwei");
Str s2("quweI");
if(s1 == s2)
cout << "Y" << endl;
else
cout << "N" << endl;
return 0;
}
输出是N