Hello there i have code like this one below and i don’t know why it doesn’t work.
class Clazz2;
class Clazz
{
public:
void smth(Clazz2& c)
{
}
void smth2(const Clazz2& c)
{
}
};
class Clazz2
{
int a,b;
};
int main()
{
Clazz a;
Clazz2 z;
a.smth(z);
//a.smth(Clazz2()); //<-- this doesn't work
a.smth2(Clazz2()); // <-- this is ok
return 0;
}
I have compilation error:
g++ -Wall -c "test.cpp" (in directory: /home/asdf/Desktop/tmp)
test.cpp: In function ‘int main()’:
test.cpp:26:17: error: no matching function for call to ‘Clazz::smth(Clazz2)’
test.cpp:26:17: note: candidate is:
test.cpp:5:7: note: void Clazz::smth(Clazz2&)
test.cpp:5:7: note: no known conversion for argument 1 from ‘Clazz2’ to ‘Clazz2&’
Compilation failed.
This is because non-constant references are not allowed to bind to temporary objects. References to
const, on the other hand, can bind to temporary objects (see 8.3.5/5 of the C++11 Standard).