Confuse the code below a bit. `
class sample
{
public:
sample() { }
sample( sample& Obj ) { }
};
void fun( sample& Obj ) { }
int main()
{
sample s(sample());
fun( sample() );
return 0;
}
we get the following error
Compilation error due to the following errors (errors). main.cpp: In the function 'int main ()': main.cpp: 29: 19: error: invalid initialization of a non-constant reference of type 'sample &' from a value of type 'sample' fun (sample ());
I figured changing the argument in the fun from & obj to const sample & obj will resolve the error.
My question is, even for the copy constructor, a temporary object has also been sent. But why the compiler did not throw the same error.
Good explanation welcomed
Relations Prasat S.
source
share