Talk:Reference (C++)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Untitled[edit]

"In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C, which is a reference in the general sense but not in the sense used by C++." huh???? —Preceding unsigned comment added by 67.188.227.244 (talkcontribs) 07:01, 16 June 2006

A "pointer" in C++ is a reference (computer science); but is never called that in C++. --Spoon! 08:44, 18 October 2007 (UTC)[reply]
This should be formulated as a definition in a less cryptic form. As it is, no beginner would even begin to understand the concept after reading the paragraph and any page on wikipedia should be accessible to all like a common encyclopedia is. Plus, one should avoid explaning a reference by saying that it is a reference datatype. This is along the lines of saying that "traffic cones are simple traffic cone thingies that can place on the road" ;) . --77.109.96.68 (talk) 00:23, 26 August 2009 (UTC)[reply]

I've deleted a section labelled "not true" which I mostly wrote, which was based on a mistaken idea of mine. --Shyland 11:55, 6 January 2007 (UTC)[reply]

Invalid References[edit]

The text states:

a reference only becomes invalid in two cases:
  • If it refers to an object with automatic allocation which goes out of scope,
  • If it refers to an object inside a block of dynamic memory which has been freed.

Not true.

int * x = 0 ;
int & y = * x ;

creating a "null" reference of sorts -- its a reference to the memory at address 0 interpeted as an "int". --BostonMA talk 01:30, 17 July 2007 (UTC)[reply]

That ought to cause a segfault on initialisation, right? So the reference doesn't become invalid, it never becomes anything at all =) —Preceding unsigned comment added by 81.216.240.6 (talk) 08:53, 17 October 2007 (UTC)[reply]
Not in most compilers. Since a reference is an address and the pointer is an address, the initialization just assigns the address of the pointer to the reference (how else would you do it?); no dereferencing is needed, and hence no segmentation fault. Most compilers represent l-values (e.g. the dereferencing of a pointer) as references anyway. --Spoon! (talk) 22:48, 25 March 2008 (UTC)[reply]

---

I'm not sure if these results are valid for all compilers, nor am I sure what the C++ specification says, but:

#include <stdio.h>

int main(int argc, char *argv[]) {
    int * x = 0;
    int & y = *x;

    fprintf(stderr, "&y = ");
    fprintf(stderr, "%d\n", &y);
    fprintf(stderr, "y  = ");
    fprintf(stderr, "%d\n", y);
}

Produces this output:

&y = 0
y  = Segmentation fault (core dumped)

At least on modern versions of GCC, references can be null and won't SEGV until you use them. This is consistent with my understanding of how references might be implemented. I believe that BostonMA is correct. TSawyer 70.101.133.253 (talk) 01:31, 2 January 2008 (UTC)[reply]

––

To anyone reading this, references can be null! Read Bjarne Stroustrup's book, the chapter on References. In reality, references were actually created to allow operator overloading, but lots of programmers make the mistake of thinking that references cannot be null and are different from pointers in that way. Wrong! They definitely can be null, you can test it out yourself by compiling and then running the following code:

int main(int argc, char *argv[])
{
    int * pi = nullptr;
    int & ri = *pi;
    ri++;
}

The code itself will compile fine, but when you run it you will get an error. Underneath it all, references are really just pointers. They may look a little different in the code, but they are still the same concept. That's why I always use pointers in my code, to keep it plain and simple, and only use references for what they were intended for, operator overloading.--RefsCanBeNull (talk) 21:11, 17 August 2016 (UTC)[reply]

More questions[edit]

The article says this is the standard layout for a reference declaration:

<Type> & <Name>

But I think the following three examples are all the same:

int& rA = A;
int & rA = A;
int &rA = A;

Am I correct?

Also: I think I'd be good if the article pointed out the differences between the different uses of "&" in the context of addresses: either it is the "address-of operator" or the "reference datatype token". --Abdull (talk) 11:25, 9 February 2008 (UTC)[reply]

polymorphism[edit]

I opened this article looking for information whether a reference in C++ supports polymorphism. But there is no "polymorphism" when i ctrl+f in this article.—Preceding unsigned comment added by 149.156.160.146 (talk) 09:14, 13 May 2009 (UTC)[reply]

There isn't, but it does. As a tip, you can ask specific questions directly at the Wikipedia:Reference desk/Computing. In addition, feel free to ask specific questions directly at my talk page. decltype (talk) 09:21, 13 May 2009 (UTC)[reply]
Added a section, giving an example about this. I also searched for this particular information. 85.187.35.160 (talk) 19:26, 13 June 2011 (UTC)[reply]

Wrong example?[edit]

In the given examples at the beginning, we can find this:

int& foo ();


lets say the body of the function is something like this:

int& foo ()
{
   int a = 1;
   return a;
}

I think this can not compile without rising a warning like: reference to a local variable.

Is there any case where we can return a reference from a function that does not take any parameters ? —Preceding unsigned comment added by 82.150.248.28 (talk) 09:38, 17 February 2010 (UTC)[reply]

Local statics is a possibility:
int& foo() { static int four = 4; return four; }
Or the more dubious:
int& foo() { int * pi = new int(4); return *pi; }
decltype (talk) 10:17, 17 February 2010 (UTC)[reply]

Hard Link Analogy[edit]

I have my doubts, that this analogy is valid. At least in the full sense as it is presented. If the original file is deleted, the hard link becomes the original. I don't think this is true for References. (But I don't know C++). So please correct the text or me if you know it better :) SumedokiN (talk) 08:02, 15 December 2011 (UTC)[reply]

Is it worth adding that a pointer has the analogy of a symbolic link in a filesystem? — Preceding unsigned comment added by 82.32.33.183 (talk) 13:39, 7 February 2012 (UTC)[reply]

Universal References[edit]

This page should be updated to include a discussion of Universal References. 72.21.196.66 (talk) 12:37, 20 June 2016 (UTC)[reply]

A potentially citable source for information: https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers 72.21.196.66 (talk) 12:37, 20 June 2016 (UTC)[reply]

Useless example?[edit]

The following example in the article, while it will compile, seems completely useless, since the incremented value of y is discarded without further use:

  • Returning a reference allows function calls to be assigned to:
 int& Preinc(int& x) {
   return ++x;  // "return x++;" would have been wrong
 }
 
 Preinc(y) = 5;  // same as ++y, y = 5</nowiki>


Could some knowledgable person replace this with an example which does something that is actually useful in real-world programs? Or, alternatively, add a short explanation why this is actually useful?

-- 194.39.218.10 (talk)

Sorry[edit]

I'm programming since about 1984 and mainly in C and C++ (for a living) but I found it hard to follow the explanations at the beginning of the article. You have to know a lot about IT technical terms and practically know what "reference" means in order to understand. Actually the word "reference" is used multiple times in order to explain "C++ reference". Yes, the reader could go upstream and read about references in general and then come back to understand the C++ case. But in my experience this practice makes Wikipedia less useful since you'll often find that the upstream article again references other parts of the related science which will force you to become a specialist in the field in order to understand anything. In some cases that might be unavoidable but here I believe that the concept of a C++ reference could be explained in a self-contained way that mainly uses normal English so that it is accessible for a wide audience and straight away. JB. --212.202.169.251 (talk) 03:00, 21 December 2023 (UTC)[reply]