Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is it a lvalue?

Is it a lvalue?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Twinsen724
    wrote on last edited by
    #1

    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

    R J 2 Replies Last reply
    0
    • T Twinsen724

      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

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      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();

      T 1 Reply Last reply
      0
      • T Twinsen724

        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

        J Offline
        J Offline
        jc0dex
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • J jc0dex

          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

          T Offline
          T Offline
          Twinsen724
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • T Twinsen724

            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

            J Offline
            J Offline
            jc0dex
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • R Rage

              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();

              T Offline
              T Offline
              Twinsen724
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • J jc0dex

                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

                T Offline
                T Offline
                Twinsen724
                wrote on last edited by
                #7

                Hi Jay, I use cl.exe of MS VC++6.0 IDE. Best regards Twinsen

                J 1 Reply Last reply
                0
                • T Twinsen724

                  Hi Jay, I use cl.exe of MS VC++6.0 IDE. Best regards Twinsen

                  J Offline
                  J Offline
                  jc0dex
                  wrote on last edited by
                  #8

                  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

                  T 1 Reply Last reply
                  0
                  • J jc0dex

                    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

                    T Offline
                    T Offline
                    Twinsen724
                    wrote on last edited by
                    #9

                    Hi Jay, Your remark is very helpful! ;) Best regards Twinsen

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups