About COM interface

Started by
2 comments, last by KevinLee 18 years, 1 month ago
Imaging the following PCode... //Cause it's PCode, just ignore all error checking.

interface IRectangle
{
   HRESULT Show();
}

class CRectangle : public IRectange
{
}

interface IRectangeList
{
   HREUSLT Count(LONG* pCount);
   HRESULT Item(LONG nIndex, IRectangle ** ppRectangle);
}

class CRectangleList : public IRectangleList
{
  HRESULT Count(LONG* pCount)
  {
      *pCount = m_arrRectangleList.size();
       return S_OK;
  }
  HRESULT Item(LONG nIndex, IRectangle** ppRectangle)
  {
     //What should be here?
  }

public:
  vector<???> m_arrRectangleList;
}

what is the correct way to implement such com interface? if i choose: vector<CRectangle*> m_arrRectangleList; HRESULT Item(LONG nIndex, IRectangle** ppRectangle) { *ppRectangle = new CRectangle(m_arrRectangleList[nIndex]); (*ppRectangle)->AddRef(); return S_OK; } I will get a compile error, cause the CRectangle is a coclass, e.g. it's an abstract class that cannot be created by operator new. So, shall I use vector<IRectangle*> ? If the answer is yes, what's the correct way to do this? And whether i just directly return the interface in my vector or make a copy? Who will manage the object? What's should i do in my destructor function? The reason that why I ask this question is I am really confused by COM interfaces. If there is a single COM interface, it would be very simple, just create it and invoke its functions. But sometimes, when using other libraries, such there are always such Obj List, what would be the correct way to write such COM Server? Thanks in advance!
Advertisement
Maybe I'm missing something here, but should you not implement all of the methods in IRectangle in CRectangle? CRectangle is your actual class that you can create. IRectangle is the interface that the client sees. Then you'd always cast IRectangle to CRectangle in your CRectangleList (provided you know that a IRectangle will always be a CRectangle, not some other class).

It's probably not there because it's psuedo-code, but don't forget to make your destructors virtual [smile]
Yes...u are right...but, you know, i just want to show a interface here, it's not a complete program, just Pseudo code.

I mean, if you were asked to provide such COM interface,

interface IRectangle
{
HRESULT GetArea(LONG* pArea);
}

interface IRectangleList
{
HRESULT Count(LONG* pCount);
HRESULT Item(LONG nIndex, IRectangle** ppRectangle);
}

How will u design com classes for this two interfaces?
That's my original question.
no idea abou it?

This topic is closed to new replies.

Advertisement