Compilation - offset of the base class pointer to the Derive class

class Base1 {
    int x;
};
class Base2 {
   int y;
};
class Derive : public Base1, public Base2 {
public:
    enum {
        PTR_OFFSET = ((int) (Base2*)(Derive*)1) - 1,
    };
};

But the compiler complains

expected constant expression

Everyone knows that the values โ€‹โ€‹of expression 4, except for the compiler, what is wrong?

How to get offset at compile time?

+4
source share
2 answers

, , (Base2*)(Derive*)1 reinterpret_casts , DyP , , . , GCC, reinterpret_cast , ( . GCC http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49171 Constexpr).

, , . / , , ( . http://www.altdevblogaday.com/2013/05/03/cc-low-level-curriculum-part-11-inheritance/). , int y Derived sizeof(x) Derived; .

, ( , ). ++ Compile-Time offset Jesse Good , GCC . , , .

++, : https://groups.google.com/a/isocpp.org/forum/#!forum/reflection.

+1

, ((int) (Base2*)(Derive*)1) - 1 , Derive : public virtual Base2. . ++.

0

All Articles