Re: Inheritance and references
On Nov 19, 11:56 am, Paul N <gw7...@aol.com> wrote:
I was under the impression that a reference had to be of
exactly the same type as the thing it was a reference to. See
for instance FAQ 8.1, which states emphatically "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."
It is the object, sort of. In the same sense as a named
variable is the object.
However, a couple of posts recently have suggested that it is
all right for a reference of one type to point to an object of
a derived type.
Using the above terminology, a reference doesn't point:-). It
may refer to a derived type, however (unlike a non-reference
named variable).
The standard goes to great lengths to say that references aren't
pointers---they don't even have an existance independently of
the object they refer to. But others have pointed out that
thinking of them as automatically dereferenced pointers results
in roughly the same semantics. It really doesn't matter much,
as long as you understand what is happening. References do
allow dynamic type and static type to differ. (Is this really
any different than allowing a const reference to refer to
a non-const object?)
I assumed at first that these were mistaken but I have tried it
out on both Turbo C++ and VC++ and it seems to work. For instance:
#include <stdio.h>
class Base { int a; };
class Derived : public Base { int b; };
int main(void) {
Derived d;
Base& br = d;
printf("Size of Base %d, Derived %d, d %d, br %d\n",
(int) sizeof(Base),
(int) sizeof(Derived),
(int) sizeof(d),
(int) sizeof(br));
}
This gives br having a size of 2, but d having a size of 4. Surely
these things can't be the same?
Expressions have both static and dynamic types. A sizeof
expression always returns the size of the static type.
I could understand it if a reference were simply a funny-looking
pointer, but the FAQ teaches strongly against this. How can things be
the same if they're different?
Because they're not the same everywhere? It's largely
a question of vocabulary, and how you want to talk about
something.
--
James Kanze