I have run this code on VS2013 and Dev-C++ but when the copy assignment doesn't return anything while actually it should, the compiler doesn't raise any error, please help me to explain this.
#include <iostream>
using namespace std;
class sample
{
public:
sample()
{
cout << "X::X()" << endl;
}
sample(sample const &)
{
cout << "X::X( X const & )" << endl;
}
sample& operator=(sample const &)
{
cout << "X::operator=(X const &)" << endl;
}
};
sample f()
{
sample tmp;
return tmp;
}
int main()
{
int a;
sample x = f();
cin >> a;
return 0;
}
if I change to:
sample x;
x = f();
VS2013 compiler will raise an error like: Error 1 error C4716: 'sample::operator=' : must return a value c:\users\xxx\desktop\test\test\main.cpp 33 1 Test