Topic: pointer to member conversion.


Author: Vladimir Neyman <Vladimir_Neyman@stratus.com>
Date: 1996/04/26
Raw View
I have the following code (details not essential to the problem are
omitted):

class A {};

class B : public A {};

struct C {
 B m_b;
}

A C::*p = &C::m_b;

The last assignement produces a compiler error in MVC:
"cannot convert from 'class B C::*' to 'class A C::*'".

At first, I thought it is a bug in MVC because a pointer to
a derived class should be convertable to a pointer to a
base class.

But then I looked into standard draft and found ($4.11) that for
pointers to members, onthe opposite, a pointer to a base class is
convertable to a pointer to a derived class. The standard says that this
inversion of normal rules  is necessary for type safety.

Does anybody know why this inversion is necessary for type safety?
Is it safe to do explicit typecast from 'class B C::*' to 'class A C::*'
in the above example?

Regards,

Vladimir Neyman
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]