Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. interface programming

interface programming

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adno
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • A Adno

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • L led mike

        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.

        A Offline
        A Offline
        Adno
        wrote on last edited by
        #3

        take a guess :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups