Salt_Peter said:
A reference is not allowed, under any circumstance, to return a size of
0.
And it definitely occupies storage since its address is the same as the
object its pointing to.
Nope: not the reference occupies memory but the object it refers to. If you
write an expression that appears to take the address of a reference, you
actually don't do that. This is why the following does not compile:
typedef int & i_ref;
int main ( void ) {
int i;
i_ref ir ( i );
i_ref* i_ref_ptr irp = &ir;
}
There are no pointers to references. If references where objects (as opposed
to the objects they refer to), we could have pointers to references.
Can i say that A a below is not an object because it doesn't occupy
storage?
It does occupy memory.
sizeof(A) > 0 is required by the standard. Instances of A will occupy
memory.
int main()
{
A a;
A& r_a(a);
std::cout << sizeof(a) << std::endl;
std::cout << sizeof(r_a) << std::endl;
}
/*
1 // <- required by law
1 // <- required by law
*/
Well, it is required that sizeof(a) > 0 and that sizeof(a) == sizeof( r_a ).
However, you are not taking the size of the reference you are taking the
size of the object a. The reference may or may not occupy memory.
Additionally, a reference that is *not* (or no longer) referencing a
valid object is undefined behaviour.
The same is not true of a pointer.
True but irrelevant for the question whether references (not the objects
they refer to) are objects.
i disaggree, a pointer is an address, period. It may or may not point
to an object.
One needs to distinguish the pointer variable (a region in memory), the
value of that variable (some bit pattern stored in that region), and the
pointee identified by the value. The first is an object. The second is a
value that may or may not point to yet another object. Most of the time, it
suffices to distinguish the first and the third and gloss over the
difference between the first and the second.
I'm not going to be convinced that a nullptr is an object, for example.
A pointer variable of type int* whose value is (int*)0 is definitely an
object (of type int*). It so happens that this int* does not point to an
object of type int.
Depends on what you mean by nullptr. A const expression such as (int*)0 does
not refer to an object but only to a possible value of an object of type
int*.
if i delete a pointer, i'm deleting the object its pointing to. In
fact, i can argue that the pointer doesn't know nor care about what
just happened. Its dumb and knows only its type unless void*.
One can argue that the pointer has "state" when its actually pointing
to something. The same can't be said of a reference: a reference
without "state" is undefined behaviour.
That doesn't mean its a valid pointer.
True.
And much less that its an object.
False: it means exactly that. You can take the address of objects and
functions; and (as far as I know) you cannot take the address of anything
else.
<Pulls out his boxing gloves, lol>
Have fun boxing Alf. You'll loose cause he is right.
Best
Kai-Uwe Bux