flow between references , constructor and initialization list

Discussion in 'C++' started by sks, Jul 11, 2006.

  1. sks

    sks Guest

    dear all,
    i have a problem . consider the following code
    class ABC
    {
    int & r;
    public: ABC( int &a) : r(a) {}
    };

    int main()
    {
    int d=10;
    ABC Myobj(d)
    }


    i cannot understand how this works and Have two question in my mind
    1) we cannot have reference to reference . but here we have a as
    refrerence to d and r is reference to a . how is this possible ?

    2) It is generally known that object is created and then constructor is
    called . and references have to be initialised immediately . so my
    question since My Obj is created then Constructor is called that means
    the object is created in the memory and still the reference is not
    initialised . How is this possible ?
     
    sks, Jul 11, 2006
    #1
    1. Advertising

  2. sks

    Guest

    sks wrote:
    > dear all,
    > i have a problem . consider the following code
    > class ABC
    > {
    > int & r;
    > public: ABC( int &a) : r(a) {}
    > };
    >
    > int main()
    > {
    > int d=10;
    > ABC Myobj(d)
    > }
    >
    >
    > i cannot understand how this works and Have two question in my mind
    > 1) we cannot have reference to reference . but here we have a as
    > refrerence to d and r is reference to a . how is this possible ?
    >

    a is more correctly a reference to an int so is r. r is not a reference
    to a, the constructor initializer list is used to initialize r with a's
    value.

    > 2) It is generally known that object is created and then constructor is
    > called . and references have to be initialised immediately . so my
    > question since My Obj is created then Constructor is called that means
    > the object is created in the memory and still the reference is not
    > initialised . How is this possible ?

    The constructor intializer list is invoked before the constructor
    (body) is invoked. So at no point withing the constructor is a
    reference uninitialized. In fact all references must be initialized by
    the constructor initializer list if they are to be used by the class.


    Thanks and regards
    SJ
     
    , Jul 11, 2006
    #2
    1. Advertising

  3. "sks" <> wrote:

    > i have a problem . consider the following code
    > class ABC
    > {
    > int & r;
    > public: ABC( int &a) : r(a) {}
    > };
    >
    > int main()
    > {
    > int d=10;
    > ABC Myobj(d)
    > }
    >
    >
    > i cannot understand how this works and Have two question in my mind
    > 1) we cannot have reference to reference . but here we have a as
    > refrerence to d and r is reference to a . how is this possible ?


    Sure you can have a reference to a reference. What do you think
    the following is:

    std::cout << a << b << c << d << std::endl;

    It's a reference to a reference to a reference to a reference to a
    reference to an iostream.

    You can chain references as many deep as you want. What really
    happens is, all levels in the chain really refer back to the
    original item:

    int a = 7;
    int & b = a; // b refers to a. b -> a
    int & c = b; // c refers to a. c -> b -> a
    int & d = c; // d refers to a. d -> c -> b -> a
    d = 32; // changes a to 32
    std::cout << a << std::endl; // prints "32"

    > 2) It is generally known that object is created and then constructor is
    > called . and references have to be initialised immediately . so my
    > question since My Obj is created then Constructor is called that means
    > the object is created in the memory and still the reference is not
    > initialised . How is this possible ?


    I'd say the object is not fully "created" until the constructor
    is finished running.

    In any case, your code makes me nervous. I don't like objects
    that have references in them to a local variable in some function.
    If that variable goes out of scope, the reference is now invalid.
    In your case, you get away with it only because the variable is in
    main().

    A reference in an object to a static variable in a function is
    safer.

    --
    Cheers,
    Robbie Hatley
    East Tustin, CA, USA
    lone wolf intj at pac bell dot net
    (put "[usenet]" in subject to bypass spam filter)
    http://home.pacbell.net/earnur/
     
    Robbie Hatley, Jul 11, 2006
    #3
  4. sks

    Daniel T. Guest

    In article <>,
    "sks" <> wrote:

    > dear all,
    > i have a problem . consider the following code
    > class ABC
    > {
    > int & r;
    > public: ABC( int &a) : r(a) {}
    > };
    >
    > int main()
    > {
    > int d=10;
    > ABC Myobj(d)
    > }
    >
    >
    > i cannot understand how this works and Have two question in my mind
    > 1) we cannot have reference to reference . but here we have a as
    > refrerence to d and r is reference to a . how is this possible ?
    >
    > 2) It is generally known that object is created and then constructor is
    > called . and references have to be initialised immediately . so my
    > question since My Obj is created then Constructor is called that means
    > the object is created in the memory and still the reference is not
    > initialised . How is this possible ?


    Your general knowledge is incorrect for C++. In C++ the object isn't
    "created" until after the constructor exists normally. If the
    constructor throws an exception, for example, the object isn't created.
     
    Daniel T., Jul 11, 2006
    #4
  5. sks

    loufoque Guest

    Robbie Hatley wrote :

    > Sure you can have a reference to a reference.


    No, you cant.

    int& & is an invalid type (for now).
     
    loufoque, Jul 11, 2006
    #5
  6. loufoque posted:

    > Robbie Hatley wrote :
    >
    >> Sure you can have a reference to a reference.

    >
    > No, you cant.
    >
    > int& & is an invalid type (for now).




    You've quoted Robbie out of context. His post made perfect sense.



    --

    Frederick Gotham
     
    Frederick Gotham, Jul 11, 2006
    #6
  7. sks

    loufoque Guest

    Frederick Gotham wrote :

    > You've quoted Robbie out of context. His post made perfect sense.


    It would be more exact to say he didn't understand what a reference to a
    reference was.
     
    loufoque, Jul 11, 2006
    #7
  8. loufoque posted:

    > Frederick Gotham wrote :
    >
    >> You've quoted Robbie out of context. His post made perfect sense.

    >
    > It would be more exact to say he didn't understand what a reference to a
    > reference was.



    This is word play.

    If you were to ask me, "Can you have a reference to a reference?", then I'd
    tell you that the question is dubious. In the following sense, you *can* have
    a reference to a reference:

    int i;

    int &r = i;

    int &s = r;

    /* "s" is a reference, and "r" is a reference.
    The thing on the left refers to the thing on
    the right. Therefore: a reference to a reference.
    */


    However, we all know that there's really no such thing as a reference... it's
    just a puff of air that mystically achieves our objective. If we re-examine
    the line:

    int &s = r;

    , then we see that "r" is in fact a reference... BUT, once you've bound a
    reference to an object, any dealings with the reference are actually dealings
    with the referred object.

    The intuitive and intellectual answer is that "No, you can't have a reference
    to a reference.", but you can't reprehend someone straight away for stating
    otherwise.

    --

    Frederick Gotham
     
    Frederick Gotham, Jul 11, 2006
    #8
  9. sks

    Ron Natalie Guest

    Robbie Hatley wrote:

    >
    > It's a reference to a reference to a reference to a reference to a
    > reference to an iostream.
    >

    Nope. You can initialize a reference from another reference, but what
    you end up with is a reference to the referred to object.
     
    Ron Natalie, Jul 12, 2006
    #9
  10. "loufoque" <> wrote:

    > Robbie Hatley wrote :
    >
    > > Sure you can have a reference to a reference.

    >
    > No, you cant.
    > int& & is an invalid type (for now).


    OK, I agree that it perhaps is best to not use the phrase
    "reference to a reference" when meaning a chain of references,
    all of which refer back to the original referent, because
    someday maybe C++ WILL allow the "int&&" type of "reference
    to a reference":

    int a = 7;
    int& b = a;
    int& c = b; // c refers to a, not to b
    int&& d = c; // d refers to c, not to a

    Perhaps I should call c "reference initialized from a reference",
    whereas the hypothetical d would be "reference to a reference".

    --
    Cheers,
    Robbie Hatley
    East Tustin, CA, USA
    lone wolf intj at pac bell dot net
    (put "[usenet]" in subject to bypass spam filter)
    http://home.pacbell.net/earnur/
     
    Robbie Hatley, Jul 14, 2006
    #10
    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.

Share This Page