I have a question about code below, exactly passing r-value arguments to function in C++.
Why we can't pass r-value object Rect
to function Print(Rect&)
, but r-value int
we can.
I know we can use Print(const int&)
or Print(int&&)
. But the main question is why my own r-value object can be passed?
Thanks in advance for explanation.
#include <iostream>
struct Rect{
uint32_t a, b;
Rect(uint32_t a, uint32_t b): a(a), b(b){
std::cout << "Rect constructor..." << std::endl;
}
~Rect(){
std::cout << "Rect destructor..." << std::endl;
}
Rect( const Rect& rhs ): a(rhs.a), b(rhs.b){
std::cout << "Rect copy constructor..." << std::endl;
}
};
void Print(Rect& r){
std::cout << "a = " << r.a << ", b = " << r.b << std::endl;
}
void Print(int& a){
std::cout << "a = " << a << std::endl;
}
int main(){
Print( Rect( 2, 5 ) );
Print( 5 );
return 0;
}