Reference to Reference, Pointer to Reference....

This is a discussion on Reference to Reference, Pointer to Reference.... within the C++ Programming forums, part of the General Programming Boards category; I have read on wiki, they say that: There shall be no references to references, no arrays of references, and ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Reference to Reference, Pointer to Reference....

    I have read on wiki, they say that:
    There shall be no references to references, no arrays of references, and no pointers to references.
    on page Reference (C++) - Wikipedia, the free encyclopedia
    I don't know why they say that, because I have do them (referencees to references, pointers to references, references to pointers and see that no error.)
    this is my code:
    Code:
    int a=6;
    int& ref_a=a;  //reference to variable
    int* ptr_a=&ref_a; //pointer to reference
    int& ref_a2=ref_a; //reference to reference
    cout<<ref_a<<endl<<*ptr_a<<endl<<ref_a2<<endl;
    And no problem appears, it will prints 6 at all.
    So, who can explain why wiki said that, am I understand wrong ? Talk to me, please.

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    Quote Originally Posted by hqt
    int* ptr_a=&ref_a; //pointer to reference
    int& ref_a2=ref_a; //reference to reference
    No, ptr_a is a pointer to an int; ref_a2 is a reference to an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    @:laserlight: Oh, I see, I'm very sorry because I have a basic mistake here
    As you know: Pointer to Pointer look like:
    Code:
    int a=5;
    int*p=&a;
    int**p=&p;
    Because above code, may I have this question: if C++ agree have <Pointer to reference> and <reference to reference>, So does It looks like:
    Code:
    int a=5;
    int &ref=a;
    int*&  ptr=&ref; //pointer to reference
    int&& ref_ref=ref;//reference to reference
    I know above code is very funny, but I still want to know: if my supposition is true, so pointer to ref and ref to ref looks like that, huh :-?

    Thanks for all

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    Quote Originally Posted by hqt
    int*& ptr=&ref; //pointer to reference
    ptr is a reference to a pointer, which is valid.

    Quote Originally Posted by hqt
    int&& ref_ref=ref;//reference to reference
    ref_ref is an rvalue reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    ptr is a reference to a pointer, which is valid.
    @laserlight: sorry, but as you say, so my code is true, huh ? But when I compiled, there two errors in line 3 and line 4
    that is:invalid initialization of non-const reference of type 'int*&' from a temporary of type 'int* and error: expected unqualified-id before '&&' token.

    Can you teach me more about this problem, please.
    thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    Quote Originally Posted by hqt
    invalid initialization of non-const reference of type 'int*&' from a temporary of type 'int*
    Pay attention to the word "temporary" to understand why

    Quote Originally Posted by hqt
    error: expected unqualified-id before '&&' token.
    Your compiler does not conform to the 2011 edition of the C++ standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and reference
    By audinue in forum C++ Programming
    Replies: 12
    Last Post: 08-15-2009, 05:42 PM
  2. reference a const reference
    By krygen in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2005, 11:37 PM
  3. reference has a rule of reference.
    By black in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 10:30 PM
  4. reference vs. const reference
    By The Monkey Hunter in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2001, 02:22 PM
  5. Pointer & reference
    By ayesha in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2001, 03:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21