interface programming
-
interface IDraw { virtual void Draw() = 0; }; interface IShapeEdit { virtual void Fill (FILLTYPE fType) = 0; virtual void Inverse() = 0; virtual void Stretch(int factor) = 0; }; //------------ // C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void Fill (FILLTYPE fType); virtual void Inverse(); virtual void Stretch(int factor); }; //----------------------------------- // Here is the global 3D rect. C3DRect* ptheRect; // Functions to operate on the 3D rect. void CreateThe3DRect(); void DestroyThe3DRect(); Implementing CreateThe3DRect() and DestroyThe3DRect() is trivial. Simply use the new and delete keywords to create and destroy the object: // Creation function. void CreateThe3DRect() { // Create a 3d-rect. ptheRect = new C3DRect(); } // Destroy the rectangle. void DestroyThe3DRect() { // See ya! delete ptheRect; //-------------------- // This method returns interfaces to the client. bool GetInterfaceFrom3DRect(INTERFACEID iid, void** iFacePtr) { if(ptheRect == NULL){ cout << "You forgot to create the 3DRect!" << endl; return false; } if(iid == IDRAW){ // They want access to IDraw. // Cast the client's pointer to the IDraw interface of ptheRect. *iFacePtr = (IDraw*) ptheRect; return true; } if(iid == ISHAPEEDIT) { // They want access to IShapeEdit. // Cast the client's pointer to the IShapeEdit interface of ptheRect. *iFacePtr = (IShapeEdit*) ptheRect; return true; } // I have no clue what they want. *iFacePtr = NULL; // Just to be safe. cout << "C3DRect does not support interface ID: " << iid << endl<< endl; return false; } //---------------------------------------------------- int main() { bool retVal = false; IDraw* pDraw = NULL; //IDraw3* pDraw3 = NULL; IShapeEdit* pShapeEdit = NULL; CreateThe3DRect(); // Can I get the IDraw interface from object? retVal = GetInterfaceFrom3DRect(IDRAW, (void**)&pDraw); if(retVal) pDraw->Draw(); DestroyThe3DRect(); return 0; } //----- are we simply casting pointers from one type to the next here? when selecting the interface with GetInterfaceFrom3DRe
-
interface IDraw { virtual void Draw() = 0; }; interface IShapeEdit { virtual void Fill (FILLTYPE fType) = 0; virtual void Inverse() = 0; virtual void Stretch(int factor) = 0; }; //------------ // C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void Fill (FILLTYPE fType); virtual void Inverse(); virtual void Stretch(int factor); }; //----------------------------------- // Here is the global 3D rect. C3DRect* ptheRect; // Functions to operate on the 3D rect. void CreateThe3DRect(); void DestroyThe3DRect(); Implementing CreateThe3DRect() and DestroyThe3DRect() is trivial. Simply use the new and delete keywords to create and destroy the object: // Creation function. void CreateThe3DRect() { // Create a 3d-rect. ptheRect = new C3DRect(); } // Destroy the rectangle. void DestroyThe3DRect() { // See ya! delete ptheRect; //-------------------- // This method returns interfaces to the client. bool GetInterfaceFrom3DRect(INTERFACEID iid, void** iFacePtr) { if(ptheRect == NULL){ cout << "You forgot to create the 3DRect!" << endl; return false; } if(iid == IDRAW){ // They want access to IDraw. // Cast the client's pointer to the IDraw interface of ptheRect. *iFacePtr = (IDraw*) ptheRect; return true; } if(iid == ISHAPEEDIT) { // They want access to IShapeEdit. // Cast the client's pointer to the IShapeEdit interface of ptheRect. *iFacePtr = (IShapeEdit*) ptheRect; return true; } // I have no clue what they want. *iFacePtr = NULL; // Just to be safe. cout << "C3DRect does not support interface ID: " << iid << endl<< endl; return false; } //---------------------------------------------------- int main() { bool retVal = false; IDraw* pDraw = NULL; //IDraw3* pDraw3 = NULL; IShapeEdit* pShapeEdit = NULL; CreateThe3DRect(); // Can I get the IDraw interface from object? retVal = GetInterfaceFrom3DRect(IDRAW, (void**)&pDraw); if(retVal) pDraw->Draw(); DestroyThe3DRect(); return 0; } //----- are we simply casting pointers from one type to the next here? when selecting the interface with GetInterfaceFrom3DRe
-
Lamefif wrote:
any easy to digest explanation would be great.
The design is lousy, period. I don't know if any explanation of why would be easy to digest since I don't have any idea what you know.