J
James Kanze
Thanks. So if I cast an object to a pointer type or other class type,
but not ref-type, then new an object is always created; in the case of
the pointer,the new object is a pointer that has the same value as the
old pointer hence they point to the same object and we can modify the
object through the new pointer. If I cast the object to a ref-type,
then there is no object created at all, not even the pointer, is this
right?
Almost. The case of casting to a reference type is a bit more
subtle; basically, "static_cast<T&>(v)" works exactly like: "T&
ref( v )", but without the name "ref". If the value being
converted is an lvalue, and reference compatible (i.e. the type
can somehow be directly bound to the reference), then there is
no new object. Otherwise, the reference must be to const, and
you may very well get a new object.