Am bit confused on the below code. `
class sample
{
public:
sample() { }
sample( sample& Obj ) { }
};
void fun( sample& Obj ) { }
int main()
{
sample s(sample());
fun( sample() );
return 0;
}
am getting the below error
Compilation failed due to following error(s). main.cpp: In function 'int main()': main.cpp:29:19: error: invalid initialization of non-const reference of type 'sample&' from an rvalue of type 'sample' fun( sample() );
I understood changing the argument in fun from sample& obj to const sample &obj will resolve the error.
My Question is, Even for copy constructor also, a temporary object has been send. But why compiler didn't throw the same error.
Nice explanation are most welcome
Regards Prasath S.