Re: well-defined terminology versus generally accepted terminology
regarding pointers and arrays
On 4/13/2011 7:26 AM, SG wrote:
On 13 Apr., 16:00, SG wrote:
If we check the standard for occurences of "pointer to" we'll find the
following things:
"if a pointer p whose static type is 'pointer to class B' is
pointing an object of class D, derived from B, the dynamic type
of the expression *p is 'D.' [...]"
So, p with a static type B* can point to an object of type D (derived
from B).
Adding to that: It's an example where "points to" has been used to
refer to a dynamic (aka runtime) property of a pointer.
Yes, but not in the same way that int* has been said to "point to" an
array. This clause, and clauses like it, are explicitly about
polymorphic objects. Polymorphic objects behave differently than other
objects.
#include <typeinfo>
#include <iostream>
struct B0 {};
struct B1 { virtual ~ B1() {}};
struct D0 : B0 {};
struct D1 : B1 {};
int main()
{
D0 d0; B0 * b0p = &d0;
D1 d1; B1 * b1p = &d1;
std::cout << typeid(*b0p).name() << std::endl;
std::cout << typeid(*b1p).name() << std::endl;
}
Compile that code and you'll see that b0p does not have any extra
information while b1p does. Only polymorphic types have "dynamic"
types. What we're really seeing here is not an exception to what
pointers point at, but the fact that when you ask a polymorphic object
for its type it will give you its "dynamic" type while a non-polymorphic
one won't even respond, leaving the compiler to interpret the request by
simply checking the type information of the pointer itself, which of
course remains B and not D.
As you'll note it actually says that, "the dynamic type of the
expression is of type D." It does not say that the type of the
expression is of type D. Dynamic lookup is a different, extra beast
allowed for and by polymorphic types. You still can't get enough
information from the pointer to call members only in the derived class,
or functions that work directly with it. You still have to cast in
order to do so.
I don't think, my interpretations from above are
in conflict with the C++ ISO standard.
In fact, I would say, that it actually supports my distinction between
"being a pointer to..." when it comes to the static type and "points
to..." when it's about a dynamic (aka runtime) property of the
pointer.
Not really. The distinction would seem to more accurately be that the
pointer "points to..." a polymorphic type and polymorphic types allow
extra RTTI look-up procedures.
--
http://crazycpp.wordpress.com