, , . , . ( ) . , , :
class Base
{
public:
int A;
int B;
}
class ChildOne : Base
{
public:
int C;
}
class ChildTwo : Base
{
public:
double C;
}
Base[10] ( 32- *) 8 : , 4- int. , ChildOne 8 , 4 C. ChildTwo 8 , 8 double C. , 8- Base, .
, , - ( 4 32- ), , . Base ChildTwo, , .
dynamic_cast Base* ChildTwo*, .
( ), :
class Data
{
public:
int A;
int B;
Data(HandlerBase* myHandler);
int DoSomething() { return myHandler->DoSomething(this) }
protected:
HandlerBase* myHandler;
}
class HandlerBase
{
public:
virtual int DoSomething(Data* obj) = 0;
}
class ChildHandler : HandlerBase
{
public:
virtual int DoSomething(Data* obj) { return obj->A; }
}
, DoSomething , ( ChildHandler), ( ). , , , - . Data , , .
, , , - .
* nitpickers: , , , , vtable, . .
II: . , , . Mea culpa, , .
- - , , . , - "", , - .
: , , , . , , , , , , . , .
, (, - ?), , . , , , , - , , , , - reinterpret_cast .
class Base
{
public:
int A;
int B;
void DoSomething();
}
class Derived : Base
{
void DoSomething();
}
void DangerousGames()
{
Base items[10];
Derived& childItem = reinterpret_cast<Derived>(items[5]);
childItem.DoSomething();
}
, reinterpret_cast , , , , : " , , , , , , ". " " , -, Base Derived, , , , , undefined. , . , , , , .