You are on page 1of 2

Collapse | Copy Code

IExampleArray*CreateArrInstance() { return new CExampleArrayImpl ; }

Now if a client wants to use the interface all it has to do is obtain a pointer to the interface object by calling the CreateArrInstance method, and then invoke the methods of the interface: Collapse | Copy Code
int UseTheInterfaceMethod() { // obtain a pointer to the interface IExampleArray* pArr = CreateArrInstance(); // invoke the interface method int iLength = pArr->getLength(); // exit successfully return 0; }

The client doesn't see the class CExampleArrayImpl or use the operator new directly. It only knows the virtual table of the IExampleArray interface. Interfaces can be inherited in the same way as classes in C++. Interface inheritance We can create another interface that will inherit from the IExampleArray and will be an extent of the IExampleArray (will have the same 2 methods as IExampleArray and will add a new method of its own): Collapse | Copy Code
class IAnOtherExampleArray : public IExampleArray { public: // the 2 methods from the interface IExampleArray must appear here as well virtual void ReverseArray() = 0; virtual int getLength() = 0; // new exposed method added by this interface virtual BOOL Find(int iKey) = 0; };

Notice that inheritance is allowed between interfaces because they are classes after all. We can now create a new co-class (implementation class for IAnOtherExampleArray interface): Collapse | Copy Code
class CExampleArrayImpl : public IAnOtherExampleArray { public: virtual void ReverseArray() { // implementation here } virtual int getLength() { // implementation here } // the new method implementation virtual BOOL Find(int iKey) { // implementation here } };

If in doubt please contact the author via the discussion board below. // obtain a pointer to the new interface IAnOtherExampleArray *pNewArr = dynamic_cast<IAnOtherExampleArray *> (pArr). A list of licenses authors might use can be found here About the Author Yaron Nir Web Developer United States Member . } Remark In order to make the above code work with the Visual Studio environment. // invoke new interface method pNewArr->Find(1). Summary By now hopefully you better understand what lies beneath the interface concept. // exit successfully return 0. In order to obtain a pointer to the new interface IAnOtherExampleArray the client must use a dynamic cast as follows: Collapse | Copy Code int UseTheInterfaceMethod() { // obtain a pointer to the interface IExampleArray* pArr = CreateArrInstance(). make sure you are compiling it with the option '/GR'. // invoke the interface method int iLength = pArr->getLength().A client that will use the above interfaces will see both IExampleArray and IAnOtherExampleArray but will not see CExampleArrayImpl and CAnOtherExampleArrayImpl co-classes. I did not cover COM issues in my article because there are excellent articles regarding the technology both in MSDN and in the developers sites. which enable the run-time type information. License This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. otherwise you will get a compilation warning (C4541).