Is it a lvalue?
-
Hi, I have a problem about this routine: #include < iostream > using namespace std; class X { public: X() { cout << "c1" << endl; } X(int i):i(i) { cout << "c2" << endl; } X(const X& x) { cout << "c3" << endl; } int i; }; X get(X i) { X x(i); return x; } int main() { X& x = get(1); x.i++; return 0; } I think after I call get(1), a temporary object X should be created, it's const so cannot be a lvalue whose value is changeable. but the code above could be compiled without any complain. Could you kindly tell me where am I wrong? Thank you very much! Best regards -- modified at 0:29 Thursday 30th March, 2006
-
Hi, I have a problem about this routine: #include < iostream > using namespace std; class X { public: X() { cout << "c1" << endl; } X(int i):i(i) { cout << "c2" << endl; } X(const X& x) { cout << "c3" << endl; } int i; }; X get(X i) { X x(i); return x; } int main() { X& x = get(1); x.i++; return 0; } I think after I call get(1), a temporary object X should be created, it's const so cannot be a lvalue whose value is changeable. but the code above could be compiled without any complain. Could you kindly tell me where am I wrong? Thank you very much! Best regards -- modified at 0:29 Thursday 30th March, 2006
Twinsen724 wrote:
where am I wrong
I am not sure, but I think that the X(const X& x) constructor takes a const parameter, but does not create a const object. What is const in your code is certainly the '1' given as parameter to the get function. This should fail:
int main() {
X y;
y.i=1;
X& x = get(y);
x.i++;
return 0;
}~RaGE();
-
Hi, I have a problem about this routine: #include < iostream > using namespace std; class X { public: X() { cout << "c1" << endl; } X(int i):i(i) { cout << "c2" << endl; } X(const X& x) { cout << "c3" << endl; } int i; }; X get(X i) { X x(i); return x; } int main() { X& x = get(1); x.i++; return 0; } I think after I call get(1), a temporary object X should be created, it's const so cannot be a lvalue whose value is changeable. but the code above could be compiled without any complain. Could you kindly tell me where am I wrong? Thank you very much! Best regards -- modified at 0:29 Thursday 30th March, 2006
Twinsen, Hey man .. looks to me like this .. If you call get(1) it returns to you the newly created x object, which you store as a reference (or even simpler "a copy"). When you go to increment the value for i it's completely valid because you initialized nothing to being constant and are simply changing the variable inside the class. Maybe I'm missing something huge, but I didn't see anything besides one of the constructors (which was never called) that takes in a const object. Hope this helps, Jay
-
Twinsen, Hey man .. looks to me like this .. If you call get(1) it returns to you the newly created x object, which you store as a reference (or even simpler "a copy"). When you go to increment the value for i it's completely valid because you initialized nothing to being constant and are simply changing the variable inside the class. Maybe I'm missing something huge, but I didn't see anything besides one of the constructors (which was never called) that takes in a const object. Hope this helps, Jay
Hi Jay, Thanks to your reply~ Your remark is reasonable. I think the key point of the problem is that what get() returns is a temporary object which is default const by the compiler, so I think it isn't a lvalue. Your reply makes me trust that the compiler in fact generates a const temporary, but because the return type is "X" other than "const X", a implicit conversion happened so that the complier didn't complain when compiling the file. Am I right? Thanks a lot! Best regards Twinsen -- modified at 21:36 Thursday 30th March, 2006
-
Hi Jay, Thanks to your reply~ Your remark is reasonable. I think the key point of the problem is that what get() returns is a temporary object which is default const by the compiler, so I think it isn't a lvalue. Your reply makes me trust that the compiler in fact generates a const temporary, but because the return type is "X" other than "const X", a implicit conversion happened so that the complier didn't complain when compiling the file. Am I right? Thanks a lot! Best regards Twinsen -- modified at 21:36 Thursday 30th March, 2006
-
Twinsen724 wrote:
where am I wrong
I am not sure, but I think that the X(const X& x) constructor takes a const parameter, but does not create a const object. What is const in your code is certainly the '1' given as parameter to the get function. This should fail:
int main() {
X y;
y.i=1;
X& x = get(y);
x.i++;
return 0;
}~RaGE();
Hi Rage, Thank you for your reply! I compiled your code, but it works. I guess the problem is the return value of "X get(X)". It generates const by the compiler because it is a temporary. But I don't write it as "const X get(X)" and I restore it as a general reference, so no error displayed while compiling. Hope my guess is right. Thanks anyway! Best regards Twinsen -- modified at 21:46 Thursday 30th March, 2006
-
Twinsen, I believe this is very likely the case, the implicit conversion may be going on behind the scenes. What compiler are you using? -- Jay
Hi Jay, I use cl.exe of MS VC++6.0 IDE. Best regards Twinsen
-
Hi Jay, I use cl.exe of MS VC++6.0 IDE. Best regards Twinsen
-
Same here. I think we're both correct here. If anyone else reading this knows otherwise please let me know :) Glad I could be a little help Twinsen! Cheers, -- Jay
Hi Jay, Your remark is very helpful! ;) Best regards Twinsen