클래서에서 operator,를 재정의해서 사용했는데, 마지막것이 반환되지 않습니다.

dltkddyd의 이미지

operator, 이란 연산자는 진술문의 마지막 결과를 반환한다고 해서 시험삼아 ComaTest란 클래스에서 이를 다시 재정의해 사용해보았습니다. 그런데 잘 첫 번째 연산만 제대로 되고, 두 번째 연산에서는

const ComaTest& operator,(const ComaTest& arg);

연산자가 호출되지 않습니다. 어찌된 영문이지 모르겠는데, 그 이유 아시는 분은 좀 알려주세요.

#include <iostream>
using namespace std;
 
template<typename CARGT>
class ComaTest {
public:
	CARGT value;
	ComaTest(CARGT arg) {
		this->value=arg;
	}
	const ComaTest& operator,(const ComaTest& arg) {
		cout<<"const ComaTest& operator,(const ComaTest& arg)  "<<this->value<<"  "<<arg.value<<endl;
		return arg;
	}
};
int main() {
	ComaTest<int> obj1(0);
	ComaTest<int> obj2(200);
	ComaTest<int> obj3(500);
	cout<<(obj1,obj2,obj3).value<<endl;
 
	return 0;
}
익명 사용자의 이미지

const ComaTest& operator,(const ComaTest& arg) const {

const reference를 반환하니 const 함수로 만들어야 연쇄적으로 호출되겠지요.

philnet의 이미지

좀 다른 얘기지만
, (쉼표) 연산자는 원래 , 연산자와 동일하게 동작하게끔 overloading 할 수 없기 때문에 overloading 하지 않는 것이 좋습니다. (내용이 궁금하시면 모어 이펙티브 C++ 7장을 참고하세요)