Operator Overloading in C++

익명 사용자의 이미지

Sorry for my poor english. I can't write the korean in my

environment.

My question is about the operator overloading in C++.

We can overload the operator '=' and '[]' respectively.

But how can I overload multiple operator just like "[]=" ??

For example, I have defined a class class_A and an instance

for class_A. And I want to use the class_A like this

ex)

int i;

class_A a;

a[0] = i;

please, give me some idea !!

thanks.

익명 사용자의 이미지

---------------------------
a[0] = i;
---------------------------

operator[]=()를 재정의 하는 것이 아니구요. 각각

--------------------------------------
배열요소타입& operator[](int i);
객체타입& operaotr=(객체타입& t);
--------------------------------------

참조변수를 리턴하면 함수가 할당 연산자(=)의 왼쪽 오퍼랜드에

오는 것이 가능합니다. 함수가 리턴되면서, 그 변수가 오기 때문이지요.

---------------
a[0] = i;
---------------
이 식은

---------------
a.operator[](0) = i;
---------------
이구요. 이때 참조를 리턴하는 operator[]가

실행되고 나면

---------------------------------------
a라는배열 객체의 0번째 요소(변수)가 리턴 = i;
---------------------------------------

그리고 i변수의 값을 할당하는 과정을 거칩니다.

결과적으로 []와 =를 재정의 하면 됩니다.