Hej
Dla tego przypadku:
#include <stdio.h>
#include <iostream>
class A {
public:
A(int x):y(x)
{
std::cout << y << std::endl;
}
bool operator==(const A& a)
{
return a.y == this->y;
}
bool operator>=(const A& a)
{
return this->y >= a.y;
}
int y;
};
int main()
{
A(6) == A(6) >= A(5); //najpierw operacja >= bo ma wiekszy priorytet niz ==
return 0;
}
output programu to 5,6,1,6
czyli niejawnie sie kontruktor stworzyl.
A dla tego błąd, trzeba operator porównywania deklarować i to poza klasą (w klasie nie pomaga):
#include <stdio.h>
#include <iostream>
class A {
public:
A(int x):y(x)
{
std::cout << y << std::endl;
}
bool operator==(const A& a)
{
return a.y == this->y;
}
bool operator>=(const A& a)
{
return this->y >= a.y;
}
int y;
};
/*bool operator==(bool x, const A& a)
{
return (int)x == a.y;
}*/ //to musze odkomentowac by nie było błędu main.cpp:40:18: error: no match for ‘operator==’ (operand types are ‘bool’ and ‘A’)
int main()
{
A(6) >= A(5) == A(6); //nie potrafi wywolac implicit kontruktora tutaj przed porównywaniem
return 0;
}
Ktoś wie czemu tak jest że w jednym przypadku nie trzeba operatora porównywania a w drugim trzeba?
A(true)
?