In visual C++ 2010, when compiling the following codes, I get error message:
static_cast error C2057: expected constant expression.
what’s wrong with that?
struct A {};
struct B : A {};
struct XX
{
static const int offset = (long)static_cast<A*>((B*)0x8) - 0x8;
};
Thanks AProgrammer, the following is correct for VC 2010:
struct A {};
struct B : A {};
struct XX
{
static const int offset;
};
const int XX::offset
= (long)static_cast<A const*>((B const*)0x8) - 0x8;
Your casts to A* and B* prevent the initializer of x to be a constant expression:
5.19/3
which is needed in that context:
9.2/4