9

Is it legal to have a pointer of a reference in C++?

For example:

int &ref = array[idx];
func(&ref);

One reason I can think of why you might want to do this if func() already exists in a library which you can't change.

Himadri Choudhury
  • 10,217
  • 6
  • 39
  • 47
  • Check this out it might help you understand. http://www.codeproject.com/KB/cpp/PtrToPtr.aspx –  Mar 21 '11 at 00:29

4 Answers4

12

It is not. The address of a reference can be taken, but "pointer to a reference of T" is not a valid type. What you are doing here is taking a pointer to the object itself, since a reference to an object simply creates another name by which you can access that same object.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 2
    No, a reference is NOT the object itself. But naming the reference results in an operation being performed on the referred-to object, in this case the address-of operation. – Ben Voigt Mar 21 '11 at 00:34
  • @Ben: A reference may be implemented in some manner (e.g. a pointer), but at the language level it is the object in the sense that the ref and the obj always behave in a perfectly identical manner. I think we agree in essence but you find the wording inappropriate, in which case I have to admit it wasn't my creation: http://www.parashift.com/c++-faq-lite/references.html#faq-8.1 – Jon Mar 21 '11 at 00:38
  • 2
    The FAQ is very good in general, but that particular answer has some unfortunate oversimplification. This error has already been beaten to death in other stack overflow questions, but the simplest proof that the reference is not the object is to consider when the destructor gets called. – Ben Voigt Mar 21 '11 at 00:41
  • http://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-c/596750#596750 – Ben Voigt Mar 21 '11 at 00:44
9

That code is legal, but it does not create a pointer to the reference. It creates a pointer to the referent (the reference target).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Pointer points to an object and reference is not an object to have pointer to it. Reference is just an alias.

This post on SO has information - Why pointers to a reference is illegal?

Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115
-1

If by "have a pointer of a reference" you mean taking the address of a reference (as in your sample: &ref), then it's perfectly legal. Any variable is an identifier, hence & can be applied by § 5.3.1-2 of C++-03. Expressions with reference types are lvalues, and thus & is applicable by the same section.

If by "have a pointer of a reference" you mean a type that's a pointer to a reference (e.g. int &*), then no, by § 8.3.2-4 (and the note at § 8.3.1-4).

outis
  • 75,655
  • 22
  • 151
  • 221
  • This doesn't answer the question. – Matthew Flaschen Mar 21 '11 at 00:31
  • there is no "taking the address of the reference". The reference is transparent, the address-of operator (&) is applied to the referred-to object. – Ben Voigt Mar 21 '11 at 00:37
  • @outis, it clearly asks about a "pointer to a reference." Your answer says nothing about pointers. – Matthew Flaschen Mar 21 '11 at 00:43
  • @Ben: an expression can have reference type. That the value of applying `&` to the reference is equal to that of applying `&` to the referent is another matter, namely the behavior rather than the legality. – outis Mar 21 '11 at 00:48
  • @Matthew: `&` results in a pointer. Pointers are implicit in my answer. What other usages pointers DasBoot might have in mind, I wouldn't hazard to guess. – outis Mar 21 '11 at 00:49
  • @outis: I agree `&` can be applied to a reference. But it doesn't take the address of the reference, it's just overloaded `operator&`. And the (default) implementation of `operator&` with a reference argument is to take the address of the referent. In any case, I'm not the one who downvoted. – Ben Voigt Mar 21 '11 at 00:50
  • @Ben: all the standard says that's relevant is "The result of the unary & operator is a pointer to its operand" and "It is unspecified whether or not a reference requires storage". Thus, when compiled and executed, whether `&` takes the address of a reference (being equal to the address of the referent) or takes the address of the referent when applied to a reference (as you describe as the default implementation of `operator&`) is an implementation detail. – outis Mar 21 '11 at 01:02
  • @outis, `&` is the address-of operator. Pointers are a compound type (See C++ §3.9.2/1) (pointer-to-X). The question asked whether there is a pointer-to-reference type, and the answer is a clear no. – Matthew Flaschen Mar 21 '11 at 01:04
  • @outis: Although references sometimes require storage and sometimes not, `&ref` NEVER (in any standard-compliant program) returns the address of storage used by the reference. By default it returns the address of the referent, but since `operator&` is one of the overloadable operators, it can do pretty much anything. The particular case of `int&` is a primitive type, so redefining operator `&` is not possible, and the address of the referent is taken. – Ben Voigt Mar 21 '11 at 01:07
  • @Matthew: DasBoot never mentioned pointer to reference types. The question phrasing is ambiguous; as a result, we each interpreted it differently. For my part, which meaning I addressed is based on the code sample. – outis Mar 21 '11 at 01:34
  • @Ben: My point in mentioning § 8.3.2-3 is not that `&ref` would return the address of the storage area used for the reference (which is clearly wrong), it's that if a reference requires storage for, say, a pointer, `&` could then examine this stored value to get the address of the referent. It would thus be examining the reference, and there would be different implementations of `operator&` for when it's applied to expressions with reference type and those without, even though the resulting value is the same. – outis Mar 21 '11 at 01:40
  • ... In this sense, you could talk about the address of a reference separate from the address of a referent, though they are equal in value. In any case, the language of the standard leaves it open to interpretation. – outis Mar 21 '11 at 01:41
  • @outis: Absolutely. But it's the same implementation when *any* operation is applied to the reference, because all cases require finding the referent. Applying `&` to a reference as in `&ref` causes the compiler to: (1) find the referent, (2) take the address of the lvalue found in step 1. At no point does the concept "address of the reference" mean anything. – Ben Voigt Mar 21 '11 at 01:43
  • @Ben: whether other operations on references must do the same is immaterial to my argument. We must hold the operation constant and vary the type, not vice-versa. My point is that `&` may have different implementations for references and for other types, thus `&` may not simply be applied to the referent. – outis Mar 21 '11 at 02:03
  • ... As for "address of the reference", it does have a meaning: it's what you get when you apply `&` to a reference, as opposed to an expression of some other type. The difference arises because we can distinguish references from other types. In the expression `&ref`, where `ref` is a reference, I can point to the identifier in source code that has a reference type and say "that is a reference, and `&` is being applied to it". ... – outis Mar 21 '11 at 02:03
  • ... Also, note my full phrasing is "taking the address of a reference". The verb is "taking the address" applied to the object "reference", not "taking" applied to the object "address of a reference". The meaning of this phrase is that `&` can be applied to an expression of reference type. ... – outis Mar 21 '11 at 02:04
  • ... I should say that I do understand your point of view, and it's one way of looking at things, but it's not the only valid way. I'm not attempting to argue that my way is the only way, merely that it's a way. It's not necessarily even the way I use all the time. – outis Mar 21 '11 at 02:04
  • @outis: But applying `&` to a reference does not "take the address of the reference". It invokes `operator&(T&)`, which for `int&` takes the address of the referent. – Ben Voigt Mar 21 '11 at 02:04
  • @outis: Also note that you're wrong when you say "`&` may have different implementations for references and for other types". If `T var; T& ref = var;`, both `&ref` and `&var` will call `operator&(T&)`. If you tried to define both `operator&(T&)` and `operator&(T)` for the same `T`, you'd get ambiguous function call errors, since neither is a better match for either `&ref` or `&var`. – Ben Voigt Mar 21 '11 at 02:07
  • @Ben: of your two most recent points, the first begs the question and doesn't fit my definition of "taking the address of the reference". As for the second, you're right about user-defined functions, but the built-in `&` operator(s) (which have been the topic of much of this discussion) might not be implemented as a function. – outis Mar 21 '11 at 02:11