reference vs object handle

Discussion in 'C++' started by terminator800tlgm@hotmail.com, Oct 31, 2003.

  1. Guest

    I don't understand the difference between ojbect handle and reference.
    For example,

    class Stack
    {
    private:
    CD cd_obj; // cd object
    DVD & dvd_ref;// dvd reference

    Why is it that dvd_ref can ONLY be initialized in the Stack's
    constructor whereas cd_obj can be initialized during the Stack
    object's lifetime?

    Also, why is not allowed to return by reference a locally created
    object?
    For example,


    Vector Vector::eek:perator+(const Vector & b) const
    {
    return Vector(x+b.x, y+b.y); //Why is this allowed?

    }

    Vector & Vector::eek:perator+(const Vector & b) const
    {
    return Vector(x+b.x, y+b.y); //Why is this NOT allowed?

    }
    , Oct 31, 2003
    #1
    1. Advertising

  2. Cy Edmunds Guest

    <> wrote in message
    news:...
    > I don't understand the difference between ojbect handle and reference.
    > For example,
    >
    > class Stack
    > {
    > private:
    > CD cd_obj; // cd object
    > DVD & dvd_ref;// dvd reference
    >
    > Why is it that dvd_ref can ONLY be initialized in the Stack's
    > constructor whereas cd_obj can be initialized during the Stack
    > object's lifetime?


    A reference must be initialized. For instance you can't write:

    int &foo;

    You would have a reference to nothing in particular which is not allowed.
    Actually this is a big advantage of references over pointers which can be
    left uninitialized.

    If dvd_ref isn't initialized in Stack's constructor it would violate this
    rule.

    >
    > Also, why is not allowed to return by reference a locally created
    > object?
    > For example,
    >
    >
    > Vector Vector::eek:perator+(const Vector & b) const
    > {
    > return Vector(x+b.x, y+b.y); //Why is this allowed?


    Consider the calling sequence:

    Vector v = v1 + v2;

    Effectively this is the same as

    Vector v = Vector(v1.x + v2.x, v1.y + v2.y);

    No problem here. The values of the temporary on the right hand side are
    copied to the values in v. (Conceptually at least -- the compiler may
    optimize that out.)

    >
    > }
    >
    > Vector & Vector::eek:perator+(const Vector & b) const
    > {
    > return Vector(x+b.x, y+b.y); //Why is this NOT allowed?


    Again, consider the calling sequence:

    Vector &v = v1 + v2;

    which is now the same as the nonsense statement

    Vector &v = Vector(v1.x + v2.x, v1.y + v2.y);

    Since the right hand side is a temporary object it can hardly be used to
    create a valid reference.

    >
    > }


    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Nov 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    625
    Joe Smith
    Jul 22, 2004
  2. Ken
    Replies:
    3
    Views:
    528
    kk_oop
    Jul 31, 2004
  3. Leon
    Replies:
    2
    Views:
    508
  4. Replies:
    1
    Views:
    468
    Mark Rae [MVP]
    Sep 20, 2007
  5. Eric Mahurin
    Replies:
    4
    Views:
    235
    David A. Black
    May 6, 2005
Loading...

Share This Page