Hi Jay, Your remark is very helpful! ;) Best regards Twinsen
Twinsen724
Posts
-
Is it a lvalue? -
Is it a lvalue?Hi Jay, I use cl.exe of MS VC++6.0 IDE. Best regards Twinsen
-
Is it a lvalue?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
-
Is it a lvalue?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
-
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