C++ reference/pointer question

Discussion in 'OT Technology' started by D1G1T4L, Jan 19, 2006.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    Can you tell me the difference between these 2
    i mean understand the 2nd code psysop holds the address of sysopref and when u derefence it, it gets the value of sysopref (am i right?)

    what does the first one do?


    Code:
    sysop &  clone(sysop & sysopref)
    {
        sysop * psysop = new sysop;
        *psysop = sysopref; //copy info
        return *psysop; //returen reference to copy
    }
    
    Code:
    sysop &  clone(sysop & sysopref)
    {
        sysop * psysop = &sysopref; 
        return *psysop;
    }
    
     
    Last edited: Jan 19, 2006
  2. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    I am not 100% sure, but I believe the second one returns a pointer that points to the address of sysopref, where the first one makes a copy of the information in sysopref.

    Therefore, for the first function, let's say I have the following:
    sysop value;
    value = clone (sysopref);
    value = blah; // this makes value = blah but sysopref still equals the initial value it had before the function call.

    Second function:
    sysop value;
    value = clone (sysopref);
    value = blah; // this makes sysopref = blah also, since both sysopref and value are pointing to the same address.

    Edited: Comments in example were mixed.
     
    Last edited: Jan 19, 2006
  3. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    MrMan is somewhat correct. In the first case you are calling the sysop copy constructor explicitly, in the second case you are calling it implicitly. I see two major problems with this design:

    1. In the second code snippet, you have a memory leak since you are reassigning the psysop pointer after you allocate its memory. I suggest using a smart pointer.

    2. In either code snippet 1 or 2, if the the sysop class is a polymorphic base class you have a bad design since you could be slicing objects at their knees. If sysop is a concrete class then you have nothing to worry about.
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i changed the code of 2nd code a little, what about now
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i understand that the first code copies over sysopref info into psysop

    so i dont understand, why MrMan said that if we change the value of psysop, it will change the value of sysopref too, since psysop doesnt point to sysopref address, i think he is wrong about that, am i right?

    Code:
    sysop &  clone(sysop & sysopref)
    {
        sysop * psysop = new sysop;
        *psysop = sysopref; //copy info
        return *psysop; //returen reference to copy
    }
    
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    In the 1st piece of code, you're making a copy of sysopref and returning a reference to the copy. In the 2nd piece of code, you're not copying anything and merely returning a reference to sysopref.

    For the sake of this example, imagine that sysop is changed to int.
    1st function would do this:

    int x = 1;
    clone(x) = 2;
    cout << x << endl;

    output would be "1" since 2 is assigned to the newly allocated int.

    2nd function:
    int x = 1;
    clone(x) = 2;
    cout << x << endl;

    output is "2" since a reference to x is returned by clone() and 2 is assigned to it.
     
    Last edited: Jan 19, 2006
  7. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    You're right, I switched the two functions in the example. Sorry for the confusion.
     
  8. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    yep thats what i thought, thanks!
     
  9. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,497
    Likes Received:
    616
    So what's the (as close as possible to) plain english difference between a pointer and a reference?
     
  10. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    a reference is a name that acts as an alisas or alterntative name for previously defined variable, which is similar to what pointers do but somehow different.
    In fact a reference is kind of like a pointer in disguise. However it is necessary to initialize the reference when you celare it; You can not delcare the reference and then assign it a value later the way you can do with a pointer

    basically a reference is like a const pointer

    int digital;

    int & otmember = digital;

    is same as int * const otmember = &digital;
     

Share This Page