operator 연산자에 대한 질문입니다.

skylandi의 이미지

class Complex
{
private:
	int Read;
	int Imag;
public:
	Complex();
	Complex(Complex &);
	~Complex();
	void operator+=(Complex &addition);
	void operator-=(Complex &subtraction);
	void operator*=(Complex &multiply);
	void operator/=(Complex &dividing);
	void operator=(Complex &substitution);
	void Set(int a,int b);
	void Get();
	int &a(int a); 
	int &a(int a,int b);
	Complex &operator+(Complex &a);
	Complex &operator+(Complex &b,Complex &c);
};

a는 function overloading 이 가능한데요
int &a(int a);
int &a(int a,int b);
Complex는 operator overloading이 가능 하지가 않습니다.
Complex &operator+(Complex &a);
Complex &operator+(Complex &b,Complex &c);

그런데 아래와 같이 frend 를 걸면 operator overloading이 가능 합니다.
그 이유가 무엇인지가 궁금 합니다.
Complex &operator+(Complex &a);
friend Complex &operator+(Complex &b,Complex &c);

좋은 하루 되시길...

markboy의 이미지

class 안에서 + operator를 overloading 할 경우 parameter는 1개 입니다.

Complex &operator+(Complex &a);

만 있다고 가정하면

Complex a, b, c;

c = a + b;

이 것은

c = a.operator+(b);

로 이해하시면 됩니다. parameter가 2개일 수는 없지요.

friend Complex &operator+(Complex &b,Complex &c);

만 사용하셨다면

c = operator+(a, b);

가 되지요. 서로 overloading 되는 대상이 달라집니다.

그런데

Complex &operator+(Complex &a); 
friend Complex &operator+(Complex &b,Complex &c);

이렇게 정의가 되면 실제 어떤 함수가 호출될지 될지 궁금하군요. :)

ixevexi의 이미지

제가 알기론 아마
먼저 클래스에서 미친듯이 찾고
그 후에 글로벌에서 찾는 걸로 알고있습니다.

Stroustrup의 책을 본지 오래라 -_- 잘...

아무튼! 정해져 있습니다.

C++, 그리고 C++....
죽어도 C++