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. Reference question

Reference question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
8 Posts 4 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.
  • L Offline
    L Offline
    LiYS
    wrote on last edited by
    #1

    It is said that "Once a reference is initialized to an object, it cannot be changed to refer to another object." But does this only apply locally. The following codes can compile successfully inside function which is controdict with the rule

    int x;
    int y;
    int& r = x;
    r = y;
    

    ----------- Another question

    extern class A;
    void h(A*& a)
    {
    ...
    }
    int main() {
    A a;
    h(&a); // Error ,Line# 2
    A *b = New A;
    h(b) // Correct

    Why line 2 can't pass the compilation? I see no difference between "&a" and "b"


    B 1 Reply Last reply
    0
    • L LiYS

      It is said that "Once a reference is initialized to an object, it cannot be changed to refer to another object." But does this only apply locally. The following codes can compile successfully inside function which is controdict with the rule

      int x;
      int y;
      int& r = x;
      r = y;
      

      ----------- Another question

      extern class A;
      void h(A*& a)
      {
      ...
      }
      int main() {
      A a;
      h(&a); // Error ,Line# 2
      A *b = New A;
      h(b) // Correct

      Why line 2 can't pass the compilation? I see no difference between "&a" and "b"


      B Offline
      B Offline
      Bhaskar
      wrote on last edited by
      #2

      There's no contradiction. Check this modified code int x = 10; int y = 20; int& r = x; // make r refer to x r = y; // this does not change the reference, value of y is copied to r, i.e., to x r += 10; // this will modify x, not y, because r still refers to x, not y cout << r << " " << x << " " << y ; // prints 30 30 20 For the second one, you have created reference to a pointer in the function parameter A*& a. By this it means a refers to a pointer of type A which it can modify. However when you pass &a to the function h, &a is not modifiable. Check this sample code A a1, a2; A* p1; A* &r1 = p1; // Ok, r1 refers to a modifiable pointer p1 p1 = &a1; // p1 points to a1 r1 = &a2; // now p1 points to a2 A* &r2 = &a2; // ERROR: &a2 is not modifiable, &a2 is a constant pointer

      L 1 Reply Last reply
      0
      • B Bhaskar

        There's no contradiction. Check this modified code int x = 10; int y = 20; int& r = x; // make r refer to x r = y; // this does not change the reference, value of y is copied to r, i.e., to x r += 10; // this will modify x, not y, because r still refers to x, not y cout << r << " " << x << " " << y ; // prints 30 30 20 For the second one, you have created reference to a pointer in the function parameter A*& a. By this it means a refers to a pointer of type A which it can modify. However when you pass &a to the function h, &a is not modifiable. Check this sample code A a1, a2; A* p1; A* &r1 = p1; // Ok, r1 refers to a modifiable pointer p1 p1 = &a1; // p1 points to a1 r1 = &a2; // now p1 points to a2 A* &r2 = &a2; // ERROR: &a2 is not modifiable, &a2 is a constant pointer

        L Offline
        L Offline
        LiYS
        wrote on last edited by
        #3

        I see your point.

        int x = 3;
        int y = 1;
        int& r = x;
        r = y;

        int main() {}

        The above codes can't pass the compilation and with a error message "'r' : missing storage-class or type specifiers", It doesn't make sense to me, here the last line just assign "y" to the object "r" refers which is "x", and when I move it to local function the error disappears, why?


        J S 2 Replies Last reply
        0
        • L LiYS

          I see your point.

          int x = 3;
          int y = 1;
          int& r = x;
          r = y;

          int main() {}

          The above codes can't pass the compilation and with a error message "'r' : missing storage-class or type specifiers", It doesn't make sense to me, here the last line just assign "y" to the object "r" refers which is "x", and when I move it to local function the error disappears, why?


          J Offline
          J Offline
          Jose Lamas Rios
          wrote on last edited by
          #4

          YongSheng Li wrote: int x = 3; int y = 1; int& r = x; r = y; int main() {} The first three lines are variable declarations that include their initialization. That's valid at the file scope. The fourth line (r = y;) is an assignment, which is not legal at the file scope; it's only valid in the body of a function. -- jlr http://jlamas.blogspot.com/[^]

          S 1 Reply Last reply
          0
          • L LiYS

            I see your point.

            int x = 3;
            int y = 1;
            int& r = x;
            r = y;

            int main() {}

            The above codes can't pass the compilation and with a error message "'r' : missing storage-class or type specifiers", It doesn't make sense to me, here the last line just assign "y" to the object "r" refers which is "x", and when I move it to local function the error disappears, why?


            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            You can only declare and initialize variables outside of functions, you can't write statements like r=y. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            1 Reply Last reply
            0
            • J Jose Lamas Rios

              YongSheng Li wrote: int x = 3; int y = 1; int& r = x; r = y; int main() {} The first three lines are variable declarations that include their initialization. That's valid at the file scope. The fourth line (r = y;) is an assignment, which is not legal at the file scope; it's only valid in the body of a function. -- jlr http://jlamas.blogspot.com/[^]

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              Beat me to it by a minute :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              J L 2 Replies Last reply
              0
              • S S Senthil Kumar

                Beat me to it by a minute :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                J Offline
                J Offline
                Jose Lamas Rios
                wrote on last edited by
                #7

                It's ok. I'll share the prize with you :) -- jlr http://jlamas.blogspot.com/[^]

                1 Reply Last reply
                0
                • S S Senthil Kumar

                  Beat me to it by a minute :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  L Offline
                  L Offline
                  LiYS
                  wrote on last edited by
                  #8

                  S. Senthil Kumar wrote: Beat me to it by a minute What that supposed to means?


                  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