+ 1

Solve this issue

This is simple c++ program, whenever it is time to call default copy constructor, instead I want to call user defined copy constructor, but it is showing error here, please check. And it works perfectly fine if I seperate declaration and function Call (see code), please give reason why it is not working https://code.sololearn.com/c4p4NNUTSKWC/?ref=app

31st Jan 2021, 2:23 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
13 Respuestas
+ 6
rajshekhar y dodamani just an addition to Ipang 's answer when you do A a = fun(); it is actually passing the return value of `fun()` into the constructor of A, that is, A::A(fun()) Now in the constructor, you are asking for the type `A&`, which accepts only lvalues, basically, values which are bound to a variable/object field. Which means that you *cannot* pass temporary values to it. Every value is 'temporary' as long as it is not bound to a variable or object. When you return a value from fun(), it is temporary as no variable or object owns it. And for this reason, passing it to the constructor of `A` gives error. Taking `const A&` type removes this restriction, and that is why the code works. My explanation of lvalues is pretty naive and they can be useful at times. See here for lvalues and rvalues https://www.internalpointers.com/post/understanding-meaning-lvalues-and-rvalues-c https://docs.microsoft.com/en-us/cpp/cpp/lvalues-and-rvalues-visual-cpp?view=msvc-160
31st Jan 2021, 3:37 AM
XXX
XXX - avatar
+ 6
Your code compiles in SL playground because of "copy elision" (https://en.cppreference.com/w/cpp/language/copy_elision) which is when your compiler decides to ignore your copy/move constructors and construct the object directly where it was supposed to be copied/moved to. If you add a print in your copy constructor you'll see that it doesn't get called. The type of copy elision happening in your code is optional to compilers. Your code does not compile in programiz because the compiler simply decides not to copy elide (different compilers/versions), so it tries to find a suitable constructor and causes the error answered by Ipang and XXX
31st Jan 2021, 4:30 AM
jtrh
jtrh - avatar
+ 3
Ipang yes, as they are not bound to anything from which you can access them. I might be wrong, but I like to think of rvalues as 'values which will be deleted when the statement is over'. For example, func("str"); Here the string was not bounded to a variable and thus when the statement is over, it will be deleted, and hence it is a rvalue. lvalues are mostly variables or object fields or members of arrays or vectors or even dereferenced pointers, anything that is held onto (I think).
31st Jan 2021, 4:17 AM
XXX
XXX - avatar
+ 2
Yeah it is running fine here, but on programiz.com online c++ compiler, it is giving error, please run it on that platform and check it.
31st Jan 2021, 3:05 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
+ 2
I see 👌 Just change line 10 by adding `const` for argument <a>. A (const A& a) { // A(A &a) {
31st Jan 2021, 3:16 AM
Ipang
+ 2
Ipang hmm now it worked, but why it so
31st Jan 2021, 3:20 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
+ 2
Adding `const` is to comply the error message "cpp:22:12: error: cannot bind non-const lvalue reference of type 'A&' to an rvalue of type 'A' " 22 | A a=fun();
31st Jan 2021, 3:30 AM
Ipang
+ 2
XXX Are literals rvalues? I have been quite confused about definition of lvalues & rvalues
31st Jan 2021, 3:48 AM
Ipang
31st Jan 2021, 4:57 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
Thank you all Ipang XXX jtrh CarrieForle
31st Jan 2021, 10:27 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
+ 1
It is not showing error, or did you change the code?
31st Jan 2021, 2:57 AM
Ipang
+ 1
XXX Thanks very much, one more doubt cleared 🙏
31st Jan 2021, 4:18 AM
Ipang
+ 1
Thanks for additional knowledge jtrh and CarrieForle 🙏
31st Jan 2021, 5:28 AM
Ipang