C++ difference between reference, objects and pointers
This is a question from an exam in an advanced course in OOP, taught in C++ (in TAU university, this semester):
Q: What is the difference between a C++ pointer and a reference?
A. A reference is the entire object while a pointer is only the address of it. B. The same meaning, and difference is only in syntax and usage. C. The syntax used to access the object. D. Pointers are simple address to the object while a reference uses the virtual table.
Which is the correct answer?
The course teacher claims that A is the correct one, and that a reference to an object is, in fact, the object itse开发者_运维知识库lf. Is that correct? I realize that accessing the reference is equivalent to accessing the object itself, however, when destructing a reference, we do not destruct the object itself. A reference is an alternative name for the object, but saying that reference==object true?
BTW, the lecturer gave the following link to an faq as support for his claim, a quote:
"Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object."
But still, I believe this is incorrect.
They're all wrong.
A reference is essentially a synonym for another object. Internally, it is often implemented as a pointer, but it has the syntax as if it were the object it refers to.
A pointer is a separate object that stores the memory address of the object it points to (or 0 if it doesn't point to an object).
You could say that the reference is the object that it refers to (it certainly acts that way), but it is not. If a reference goes out of scope then the object it refers to is not destructed, so the reference is not the object.
There's a distinction between a reference and an object - you can have multiple references to the same object. An object has 'identity', while a reference itself doesn't really.
While the mechanics of a reference are quite different than those of a pointer, I'd say that conceptually, a reference is actually quite similar to a pointer.
(B) is the closest, but still not exactly correct. A reference is syntactic sugar for a const pointer. Just like a const pointer, it must be bound to an lvalue at initialization, and can never be rebound. Just like a const pointer, it is polymorphic.
EDIT: Short proof that (A) is incorrect, since at least a couple people are defending it:
struct A { int x; int f() { return 1; } }
struct B : public A { int y; int f() { return 2; } }
B b;
A& a = b;
assert(sizeof(a) == sizeof(b)); // fail
assert(a.f() == b.f()); // fail again
Just to climb on my hobby-horse for a moment, Universities have no business in teaching people how to program in specific computer languages. The fact that they do is simply an indicator of how degraded they have become over the past 30 years. I worked for the University of London from 1979 to 1983 as a microbiology technician and programmer, and the microbiology students (note not CS students!) were expected to pick up how to use the computers and to program more or less on their own, which they did, as much as they needed to.
But nowadays even CS students seem to be spoonfed everything, and tested on that "knowledge" by almost impossible to fail tests like the one quoted by the OP.
Gah!!!
An important thing is to differentiate between:
- Reference as an expression
- Reference itself
The FAQ and the teacher seem to talk about the first point, but the teacher's question is formulated as if it were asking about the second point. To explain the point of view of the FAQ, consider what the Standard describes as the very first stage of expression processing
If an expression initially has the type "reference to T" (8.3.2, 8.5.3), 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
After this transformation, a reference and the object or function it designates cannot be distinguished anymore using that expression. But that doesn't make a reference equivalent to an object. The former just refers to the latter. Even less so since references can also refer to functions.
Now, a reference itself is just that - an entity that happens to refer to an object or function but that doesn't store something on its own.
The same mistake in arguing is sometimes made by people when they say that arrays in C would be just pointers. What they really mean is that arrays in expressions are (mostly) just pointers. But that doesn't make both of them equal in their own right.
All answers are incorrect, A is closest.
Pointer is address of object which is object itself.
"Object" is "something" somewhere in a memory. Class instance, int, float, etc.
Reference is an alternative way of accessing an object. It is reference to an object, but not the object itself. It may or may not be implemented as pointer. You may think about it as an alternative object name, but this isn't exactly right. The closest correct descriptions I can think of is "alternative interface for accessing/manipulating object"(unfortunately "interface" sounds misleading if you take OOP in account, although it is (IMO) most correct one).
Answering to whether the teacher is wrong: Simple logic. Strictly speaking, it is what the name (and the standard) say: It is a reference to (="a name of") the object, NOT the object itself. As said before, when the reference variable runs out of scope, the object's destructor is not called, therefore the reference is not the object.
#include <iostream>
class A {
public:
~A()
{
std::cout << "~A() called.\n";
}
};
void f(A &a)
{
// a running out of scope...
}
int main()
{
A a;
std::cout << "calling f()\n";
f(a);
std::cout << "done calling f()\n";
return 0;
}
One of the big differences between pointers and references that I haven't seen mentioned is that a pointer can be NULL while a reference can't. That doesn't mean the object a reference pointers to can't go out of scope resulting in the same types of issues people have with pointers, merely that there is no "no assigned" state.
I believe your teacher is confusing a metaphor and a concrete statement. A reference definitely is not the actual object, and it is implemented as a "funny looking pointer" but the point of that statement is that you are to think of a reference as the actual object being referenced, you are not to think or program as if you are handling a pointer. Syntactically, accessing a reference to an object is identical to accessing the object itself, except in a few cases mentioned in the comments below.
Let's evaluate one option at a time -
A. A reference is the entire object while a pointer is only the address of it.
There is no such a thing as "entire object"! it is just the object. Many references can point object and then cease to refer to it even though object itself continues to persist. Wrong Answer!
B. The same meaning, and difference is only in syntax and usage. It is NOT the same thing. For example, when you get &ref - you cannot do ref++ where as if you get *ptr, you can do ptr++. Pointer not only allow you to access objects but allows pointer arithmetic; thereby with pointers, you can pass not only 1 address (and modify that address) but the entire array of arbitrary location with the same syntax. You cannot do this with reference! Wrong Ans!
C. The syntax used to access the object. Didn't quite get how it constitutes difference between two concept. Wrong Ans!
D. Pointers are simple address to the object while a reference uses the virtual table. I don't thing there is anything called virtual table. Pointers are usually pointing in heap, where as reference is a pointer sitting inside the stack. Wrong Ans!
All are wrong...
Dipan.
The question isn't about objects and references, it's about pointers and references. The point is that pointers denote a memory location and references denote an object- a higher level semantic construct. Simple as that.
And the teacher already gave you the answer anyhow: A -> correct answer.
Good luck in your studies.
精彩评论