#include <iostream>
#include<string>
using namespace std;
class MyClass
{
public:
MyClass(int a,string b)
{
one = a;
two = b;
}
bool operator==(MyClass* a)
{
if (one == (a->one) && two == (a->two))
{
return true;
}
return false;
}
private:
int one;
string two;
};
void text()
{
MyClass lx(10,"aaa");
MyClass lxx(10, "aaa");
if (lx.operator==(&lxx))
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
int main()
{
text();
return 0;
}
正常 打印true;
看了下简化的:
#include <iostream>
#include<string>
using namespace std;
class MyClass
{
public:
MyClass(int a,string b)
{
one = a;
two = b;
}
bool operator==(MyClass& a)
{
if (one == (a.one) && two == (a.two))
{
return true;
}
return false;
}
private:
int one;
string two;
};
void text()
{
MyClass lx(10,"aaa");
MyClass lxx(10, "aaa");
if (lx==lxx)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
int main()
{
text();
return 0;
}
正常,打印true;
想了下改一改:
#include <iostream>
#include<string>
using namespace std;
class MyClass
{
public:
MyClass(int a,string b)
{
one = a;
two = b;
}
bool operator==(MyClass*a)
{
if (one == (a->one) && two == (a->two))
{
return true;
}
return false;
}
private:
int one;
string two;
};
void text()
{
MyClass lx(10,"aaa");
MyClass lxx(10, "aaa");
if (&lx==&lxx)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
int main()
{
text();
return 0;
}
感觉怪怪的,果然是false
又想了一下改了改,把text里面&lx==&lxx改成lx==&lxx,就是true了...
lx==&lxx展开应该是lx.operator==(&lxx)
那这&lx==&lxx展开应该是个啥玩意