Reference to a reference

Discussion in 'C++' started by Senthil, Oct 27, 2005.

  1. Senthil

    Senthil Guest

    Hi,
    I am reading Modern C++ Design where Anderi quoted "C++ does not allow
    references to references".

    Assume i have
    DoThat(const string& strData2)
    {
    ...
    }

    DoThis(const string& strData)
    {
    ..
    DoThat(strData);
    }

    int main()
    {
    string Data;
    DoThis(Data);
    }

    Now the strData i have inside DoThis is a reference to Data.The
    strData2 I have is a reference to strData.i.e strData2 is a reference
    to a reference.
    Here Andrei's statement confuses me..Am i missing something?

    Thanks,
    Senthil
    Senthil, Oct 27, 2005
    #1
    1. Advertising

  2. Senthil

    mlimber Guest

    Senthil wrote:
    > Hi,
    > I am reading Modern C++ Design where Anderi quoted "C++ does not allow
    > references to references".
    >
    > Assume i have
    > DoThat(const string& strData2)
    > {
    > ...
    > }
    >
    > DoThis(const string& strData)
    > {
    > ..
    > DoThat(strData);
    > }
    >
    > int main()
    > {
    > string Data;
    > DoThis(Data);
    > }
    >
    > Now the strData i have inside DoThis is a reference to Data.The
    > strData2 I have is a reference to strData.i.e strData2 is a reference
    > to a reference.
    > Here Andrei's statement confuses me..Am i missing something?
    >
    > Thanks,
    > Senthil


    What's the context (section number and paragraph)? Perhaps he means
    that you can't do with references what you can do with pointers:

    int anInt = 42;
    int *pointer_to_int = &anInt;
    int **pointer_to_pointer_to_int = &pointer_to_int;

    Cheers! --M
    mlimber, Oct 27, 2005
    #2
    1. Advertising

  3. Senthil

    Dan Cernat Guest

    Senthil wrote:
    > Hi,
    > I am reading Modern C++ Design where Anderi quoted "C++ does not allow
    > references to references".
    >
    > Assume i have
    > DoThat(const string& strData2)
    > {
    > ...
    > }
    >
    > DoThis(const string& strData)
    > {
    > ..
    > DoThat(strData);
    > }
    >
    > int main()
    > {
    > string Data;
    > DoThis(Data);
    > }
    >
    > Now the strData i have inside DoThis is a reference to Data.The
    > strData2 I have is a reference to strData.i.e strData2 is a reference
    > to a reference.

    Here you are wrong. strData2 is a reference to a string according with
    its declaration.

    > Here Andrei's statement confuses me..Am i missing something?
    >
    > Thanks,
    > Senthil


    /dan
    Dan Cernat, Oct 27, 2005
    #3
  4. Senthil

    Guest

    mlimber wrote:
    > you can't do with references what you can do with pointers:
    >
    > int anInt = 42;
    > int *pointer_to_int = &anInt;
    > int **pointer_to_pointer_to_int = &pointer_to_int;


    than what are references are good for? what can be done with
    references, that cannot be done with pointers?
    , Oct 27, 2005
    #4
  5. * Senthil:
    >
    > I am reading Modern C++ Design where Anderi quoted "C++ does not allow
    > references to references".
    >
    > Assume i have
    > DoThat(const string& strData2)
    > {
    > ...
    > }
    >
    > DoThis(const string& strData)
    > {
    > ..
    > DoThat(strData);
    > }
    >
    > int main()
    > {
    > string Data;
    > DoThis(Data);
    > }
    >
    > Now the strData i have inside DoThis is a reference to Data.The
    > strData2 I have is a reference to strData.i.e strData2 is a reference
    > to a reference


    No, strdata2 refers to the same object that strData refers to.


    > Here Andrei's statement confuses me..Am i missing something?


    Yes, in addition to the above, and what Andrei's sentence is all about:
    that C++ doesn't currently allow the syntax T&&, or U& when U is a
    reference type, which is the probem for template code (it can be solved,
    as Andrei shows, but it's awkward).

    Bjarne Stroustrup has suggested (possibly this was just with regard to
    template code) that T&& should be allowed, and defined to mean T&.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 27, 2005
    #5
  6. Senthil

    Rolf Magnus Guest

    Senthil wrote:

    > Hi,
    > I am reading Modern C++ Design where Anderi quoted "C++ does not allow
    > references to references".
    >
    > Assume i have
    > DoThat(const string& strData2)


    According to the declaration, strData2 is a reference to const string.

    > {
    > ...
    > }
    >
    > DoThis(const string& strData)


    According to the declaration, strData is a reference to const string.

    > {
    > ..
    > DoThat(strData);
    > }
    >
    > int main()
    > {
    > string Data;
    > DoThis(Data);
    > }
    >
    > Now the strData i have inside DoThis is a reference to Data.The
    > strData2 I have is a reference to strData.


    It's a refernce to Data.

    > i.e strData2 is a reference to a reference.


    No. If allowed, a reference to a reference would be declared something like:

    const string&& strData2
    Rolf Magnus, Oct 27, 2005
    #6
  7. Senthil

    Guest

    , Oct 27, 2005
    #7
  8. Senthil

    Guest

    , Oct 27, 2005
    #8
  9. Senthil

    Senthil Guest

    Thanks for all your replies..Just to make clear that i've understood it
    properly,
    The following should be invalid program right ?

    int main()
    {
    int i = 10;
    int &r1 = i;
    int &r2 = r1;

    return 0;
    }

    Best Regards,
    Senthil
    Senthil, Oct 27, 2005
    #9
  10. Senthil

    mlimber Guest

    wrote:
    > wrote:
    > > mlimber wrote:
    > > > you can't do with references what you can do with pointers:
    > > >
    > > > int anInt = 42;
    > > > int *pointer_to_int = &anInt;
    > > > int **pointer_to_pointer_to_int = &pointer_to_int;

    > >
    > > than what are references are good for? what can be done with
    > > references, that cannot be done with pointers?

    >
    > http://www.parashift.com/c -faq-lite/references.html
    >
    > particularly
    >
    > http://www.parashift.com/c -faq-lite/references.html#faq-8.6


    Also, references are necessary for operator overloading:

    #include <ostream>
    using namespace std;

    class Foo { /* ... */ };

    ostream& operator<< ( ostream& os, const Foo& foo )
    {
    // Stream elements of Foo here ...
    return os;
    }

    void Bar( const Foo& foo )
    {
    cout << "Here's your Foo:\n" << foo << endl;
    // ...
    }

    We couldn't do this with pointers.

    Cheers! --M
    mlimber, Oct 27, 2005
    #10
  11. <> schrieb im Newsbeitrag
    news:...
    > > particularly
    > > http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >
    > so, what's your point? what can be done with reference and can not be
    > done with pointer, again?
    >


    Nothing. AFAIK references are usually implemented as pointers.
    But references exist because they are more convenient to use because
    references can't be NULL.

    Greetings Chris
    Christian Meier, Oct 27, 2005
    #11
  12. Senthil

    mlimber Guest

    Senthil wrote:
    > Thanks for all your replies..Just to make clear that i've understood it
    > properly,
    > The following should be invalid program right ?
    >
    > int main()
    > {
    > int i = 10;
    > int &r1 = i;
    > int &r2 = r1;
    >
    > return 0;
    > }
    >
    > Best Regards,
    > Senthil


    No. That is valid. Both r1 and r2 refer to the same object (in this
    case, i).

    Cheers! --M
    mlimber, Oct 27, 2005
    #12
  13. "Senthil" <> schrieb im Newsbeitrag
    news:...
    > Thanks for all your replies..Just to make clear that i've understood it
    > properly,
    > The following should be invalid program right ?
    >
    > int main()
    > {
    > int i = 10;
    > int &r1 = i;
    > int &r2 = r1;
    >
    > return 0;
    > }
    >
    > Best Regards,
    > Senthil
    >



    No, this program is valid. r2 is a reference to an int as you can see in
    your declaration of r2:
    > int &r2 = r1;


    But as already mentioned before, you can't do something like
    int&& r2 = r1;

    Greetings Chris
    Christian Meier, Oct 27, 2005
    #13
  14. Senthil

    Guest

    mlimber wrote:
    > references are necessary for operator overloading:
    > We couldn't do this with pointers.


    okay, agreed...

    though this is because of the way operators are "implemented" or
    "defined" in c++. I can imagine a language, where there would be no
    difference between "variables" and "pointers" presented to programmer,
    so that constants would be only objects....
    , Oct 27, 2005
    #14
  15. Senthil

    Guest

    wrote:
    > > particularly
    > > http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >
    > so, what's your point? what can be done with reference and can not be
    > done with pointer, again?


    You've snipped a lot of context. You originally asked two questions.

    1 what are references are good for?
    2 what can be done with references, that cannot be done with pointers?

    faq 8.6 answers question 1.

    I also pointed you at all of section 8
    http://www.parashift.com/c -faq-lite/references.html
    Read this whole section and you will find the answer to question 2 (e.g
    faq 8.3).

    HTH
    Gavin Deane
    , Oct 27, 2005
    #15
  16. Senthil

    Guest

    Christian Meier wrote:
    > because
    > references can't be NULL.


    how about:
    int* i = 0; int &j = *i;

    it gives me no errors, no warnings.
    , Oct 27, 2005
    #16
  17. Senthil

    Guest

    oh wait. then we would loose math operations with pointers.
    , Oct 27, 2005
    #17
  18. Senthil

    Guest

    wrote:
    > Christian Meier wrote:
    > > because
    > > references can't be NULL.

    >
    > how about:
    > int* i = 0; int &j = *i;
    >
    > it gives me no errors, no warnings.


    So you compiled it. Did you run it?

    Gavin Deane
    , Oct 27, 2005
    #18
  19. Senthil

    Guest

    wrote:
    > wrote:
    > > Christian Meier wrote:
    > > > because
    > > > references can't be NULL.

    > >
    > > how about:
    > > int* i = 0; int &j = *i;
    > >
    > > it gives me no errors, no warnings.

    >
    > So you compiled it. Did you run it?
    >
    > Gavin Deane


    yes, it runs fine.
    there can't be error until you USE j. and I do not :p
    , Oct 27, 2005
    #19
  20. Senthil

    Andre Kostur Guest

    wrote in news:1130423577.642411.300190
    @g43g2000cwa.googlegroups.com:

    >
    > wrote:
    >> Christian Meier wrote:
    >> > because
    >> > references can't be NULL.

    >>
    >> how about:
    >> int* i = 0; int &j = *i;
    >>
    >> it gives me no errors, no warnings.

    >
    > So you compiled it. Did you run it?


    It may run too (without crashing). Doesn't matter. Undefined Behaviour
    has been invoked by dereferencing a NULL pointer. To the OP: remember that
    when one says that references may not be "NULL", there is an implicit "in a
    well-formed C++ program" attached to the end of the statement. By
    dereferencing a NULL pointer, the program is no longer well-formed. Try to
    make a reference to "NULL" without invoking undefined behaviour first.
    Andre Kostur, Oct 27, 2005
    #20
    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. Darren
    Replies:
    0
    Views:
    481
    Darren
    Oct 11, 2004
  2. Dude
    Replies:
    0
    Views:
    337
  3. ce
    Replies:
    1
    Views:
    2,960
  4. sam pal
    Replies:
    3
    Views:
    524
    E. Robert Tisdale
    Jul 16, 2003
  5. Craig Nicol
    Replies:
    2
    Views:
    454
    Craig Nicol
    May 10, 2004
Loading...

Share This Page