gccでは通らないけどVisualStudio2008では通るコード

先生がはまってたので簡単に書いてみた.これなんでダメになるのかな?ちなみにNGな方のエラーメッセージはこちら.

test.cpp: In function ‘int main()’:
test.cpp:19: error: no match for ‘operator=’ in ‘t1 = Test()’
test.cpp:9: note: candidates are: Test& Test::operator=(Test&)

コンストラクタを直接呼んでそれをoperator=に突っ込むときはconstが必要らしい?まぁ常にconst付けてれば問題ないんですが.

OKなコード

#include <iostream>
class Test{
	public:
		Test(){}
		Test(const Test& other){}
		~Test(){}
		const Test& operator=(const Test& other){
			return *this;
		}
};
int main(){
	Test t1;
	Test t2(t1);
	t1=Test();
	return 0;
}

NGなコード

#include <iostream>
class Test{
	public:
		Test(){}
		Test(Test& other){}
		~Test(){}
		Test& operator=(Test& other){
			return *this;
		}
};
int main(){
	Test t1;
	Test t2(t1);
	t1=Test();
	return 0;
}

コメントする