2011.11.30.C++ for Intel
.pdfКак было и как стало
Object *pObj;
…
pObj = malloc(sizeof(Object)); PrepareFunctionPointers(pObj, Triangle);
res = pObj->table->Init(pObj, …); pObj->table->Draw(pObj, …);
pObj->table->Close(pObj, …); free(pObj);
Object *pObj;
…
pObj = new Triangle;
res = pObj->Init(…); pObj->Draw(…); delete pObj;
Как стало на С++
delete pObj; mov ecx, [pObj]
call pObj->table->~Triangle() free(pObj)
Как стало на С++ |
Triangle:: |
|
|
|
Triangle |
|
|
struct FuncTable for Triangle
Triangle::Init
Triangle::Dra
w
Triangle::~
Как стало на С++
Object *pObj;
…
pObj = new Triangle;
res = pObj->Init(…); pObj->Draw(…); delete pObj;
Как стало на С++
class Square |
class Circle |
class Triangle |
{ |
{ |
{ |
public: |
public: |
public: |
// Constructor |
// Constructor |
// Constructor |
Square(void); |
Circle(void); |
Triangle(void); |
// Destructor |
// Destructor |
// Destructor |
~Square (void); |
~Circle(void); |
~Triangle(void); |
// Initialize the object |
// Initialize the object |
// Initialize the object |
res Init(…); |
res Init(…); |
res Init(…); |
// Draw the object |
// Draw the object |
// Draw the object |
void Draw(…); |
void Draw(…); |
void Draw(…); |
// Close the object |
// Close the object |
// Close the object |
void Close(…); |
void Close(…); |
void Close(…); |
}; |
}; |
}; |
С++: наследование
class Object |
class Circle |
class Circle : public Object |
{ |
{ |
{ |
public: |
public: |
public: |
|
// Constructor |
// Constructor |
|
Circle(void); |
Circle(void); |
// Destructor |
// Destructor |
// Destructor |
virtual |
|
virtual |
~Object(void) = 0; |
~Circle(void); |
~Circle(void); |
// Initialize the object |
// Initialize the object |
// Initialize the object |
virtual |
|
virtual |
res Init(…) = 0; |
res Init(…); |
res Init(…); |
// Draw the object |
// Draw the object |
// Draw the object |
virtual |
|
virtual |
void Draw(…) = 0; |
void Draw(…); |
void Draw(…); |
// Close the object |
// Close the object |
// Close the object |
virtual |
|
virtual |
void Close(…) = 0; |
void Close(…); |
void Close(…); |
}; |
}; |
}; |
С++: полиморфизм
Object
Circle |
Square |
Triangle
void PrintArea(Object *pObj)
{
printf("Object area is %u\n", pObj->Area());
}
С++: инкапсуляция
Object *pObj;
…
pObj = newCreateTriangle();Triangle;
res = pObj->Init(…); pObj->Draw(…); delete pObj;
C++:наследование
Object |
Circle |
Ball |
class Object |
class Circle : public Object |
class Ball : public Circle |
{ |
{ |
{ |
public: |
public: |
public: |
… |
… |
// Destructor |
}; |
}; |
virtual |
|
|
~Ball(void) ; |
|
|
// Draw the ball |
|
|
virtual |
|
|
void Draw(…); |
|
|
// Get the volume |
|
|
virtual |
|
|
int Volume(…); |
|
|
}; |
C++:наследование
struct FuncTable for Ball |
Ball::Ball |
|
|
|
|
|
|
|
|
|
|
|
|
|
Ball::Draw
|
Ball::~ |
struct FuncTable for Circle |
Ball::Volume |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Circle:: Circle |
|
Circle::~ |
|
Circle::Draw |
|
Circle::Init |
||
|
|
|
|
|
|
|
|
|