Why Are Pointers to a Reference Illegal in C++

Why are pointers to a reference illegal in C++?

A pointer needs to point to an object. A reference is not an object.

If you have a reference r, once it is initialized, any time you use r you are actually using the object to which the reference refers.

Because of this, you can't take the address of a reference to be able to get a pointer to it in the first place. Consider the following code:

int x;
int& rx = x;

int* px = ℞

In the last line, &rx takes the address of the object referred to by rx, so it's exactly the same as if you had said &x.

Pointer to member that is a reference illegal?

Member pointer (as opposed to a simple pointer to a member) is simply an offset into the structure, not a pointer at all. You can get data through it only in conjunction with the structure itself (or a pointer to a structure): the value of the offset is added to the address of the structure, and the result is dereferenced to produce the value of the member.

Now suppose a member is a reference, so accessing data through it already requires a dereference (compiler hides it from us, but it needs to spit out the corresponding instructions in its output). If C++ were to allow member pointers to references, they'd be of yet another type: an offset that needs to be added to the base, and then dereferenced twice. It is too much work to improve an already obscure feature; prohibiting it is a much better way out.

illegal instruction occur while using pointer and reference

I tried to change ptr = &data; to *ptr = data;, and ran again the code, the error("illegal instruction") occurred.

The problem is that the the pointer ptr was uninitialized(and does not point to any int object) and so dereferencing that pointer(which you did when you wrote *ptr on the left hand side) leads to undefined behavior.

    int *ptr;   //pointer ptr does not point to any int object as of now    
*ptr = data;
//-^^^^--------->undefined behavior since ptr doesn't point to any int object

To solve this make sure that before dereferencing ptr, the pointer ptr points to some int object.

void pt_ref(int& data) 
{
int var = 10; //int object
//-------------vvvv-------->now ptr points to "var"
int *ptr = &var;
//--vvvv------------------->this is fine now
*ptr = data;
}

Difference between pointer to a reference and reference to a pointer

First, a reference to a pointer is like a reference to any other variable:

void fun(int*& ref_to_ptr)
{
ref_to_ptr = 0; // set the "passed" pointer to 0
// if the pointer is not passed by ref,
// then only the copy(parameter) you received is set to 0,
// but the original pointer(outside the function) is not affected.
}

A pointer to reference is illegal in C++, because -unlike a pointer- a reference is just a concept that allows the programmer to make aliases of something else. A pointer is a place in memory that has the address of something else, but a reference is NOT.

Now the last point might not be crystal clear, if you insist on dealing with references as pointers. e.g.:

int x;
int& rx = x; // from now on, rx is just like x.
// Unlike pointers, refs are not real objects in memory.
int* p = &x; // Ok
int* pr = ℞ // OK! but remember that rx is just x!
// i.e. rx is not something that exists alone, it has to refer to something else.
if( p == pr ) // true!
{ ... }

As you can see from the above code, when we use the reference, we are not dealing with something separated from what it refers to. So, the address of a reference is just the address of what it refers to. Thats why there is no such thing called the address of the reference in terms of what you are talking about.

c++ pointer to a reference

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

Why is this Pointer to Constant Pointer assignment Illegal?

Something you need to get used to with pointer declarations is that you need to try reading them right to left. What matters is what's on each side of the *

int * const p means p is a const pointer to a non-const int

const int **p1 means p1 is a non-const pointer to a non-const pointer to a const int

Your second declaration fails because it creates a non-const variable from a const one.

Why is it illegal/immoral to reseat a reference?

Because there is no syntax to do it:

int x = 0;
int y = 1;
int & r = x;

Now if I say:

r = y;

I assign the value of y to x. If I wanted to reseat I would need some special syntax:

r @= y;    // maybe?

As the main reason for using references is as parameters and return types of functions, where this is not an issue, it didn't seem to C++'s designers that this was a path worth going down.

Is it legal c++ to use reference as array/pointer?

The question of code readability aside,

is it guaranteed that a pointer to a reference of an array points to the original array?

Yes, see § 5.5 Expressions:

If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression.

And §8.3.2 References:

4   It is unspecified whether or not a reference requires storage.

5   There shall be no references to references, no arrays of references, and no pointers to references.

In other words, an "address of a reference" isn't a thing; given double& dref, taking an address &dref will give the address of the original element inside the array.



Related Topics



Leave a reply



Submit