Код: Выделить всё
struct BaseClass {
protected:
int offset_of_derived;
public:
template
T* getAs() { return (T*)((char*)this + offset_of_derived); }
protected:
char* getBasePtr() { return (char*)this; }
};
template
struct DerivedClass : BaseClass
{
DerivedClass() : BaseClass() { this->offset_of_derived = (char*)this - BaseClass::getBasePtr(); }
};
Код: Выделить всё
struct BaseClass {
private:
int offset_of_derived;
public:
template
T* getAs() { return (T*)((char*)this + offset_of_derived); }
private:
char* getBasePtr() { return (char*)this; }
};
template
struct DerivedClass : BaseClass
{
DerivedClass() : BaseClass() { this->offset_of_derived = (char*)this - BaseClass::getBasePtr(); }
};
Подробнее здесь: https://stackoverflow.com/questions/791 ... pe-pointer