Martin,
A COM Interface is an abstract service. Such a service can be easily
related to an object as we know it but is not the object.
A COM class is a named set of these Interfaces in a DLL/EXE. A COM
object is an abstraction for whatever implements these interfaces in
the DLL/EXE. In your example, ITree and IGrow interfaces would be in
the same COM class; IApple would be in another.
Your ITree is just one of the service interfaces in a hypothetical COM
class named CLSID_Tree, for example. ITree is out on the end of the
lollipop; it is neither the tree object nor a surrogate for the class.
A COM object is not an object as we know it in C++ or any other object
oriented technology in the last 25 years. It is more fundamental. Ir
ia more abstract. It is focused on the interfaces.
For example, think of C++ classes TMyTree and TMyApple rather than
ITree and IApple. Then, provide a service IGrow as a wrapper for a set
of methods in TMyTree. The IGrow service interface need not be limited
to trees, but could possibly be used for feathers, fur and anything
else that could implement an IGrow interface.
You can model IGrow in C++ by a 100% pure abstract base class TMyGrow
that you inherit into TMyTree. Note that TMyGrow could be inherited
into TMyBird, TMyMammal or even TMyJunkPile.
The COM interface IGrow is a wrapper for TMyGrow. In IGrow, you will
need to move the normal return result into the argument of the IGrow
interface because every COM method returns an HRESULT to be tested.
Some people use the COM methods as they would the C++ methods, but
this is problematic - the interface service classes and the object
model do not always map as cleanly as they do in this case.
OK, so how do you tell an IGrow for trees from an IGrow for birds? You
put them into different COM classes with different names such as
CLSID_Tree and CLSID_Bird.
If you use ATL, you will get default interfaces ITree, IDispatch, etc.
in default modules Tree.hpp/cpp inherited into a default C++ class
CTree that does most of the COM interfacing work for you. You will
override FinalConstruct() in class CTree to construct TMyTree. ITree
does little more
Interface IGrow will be in class CLSID_Tree. In ATL, you would make it
one of the base classes of CTree along with all the other interfaces
of CLSID_Tree. In CTree, IGrow methods wrapper the abstract class
TMyGrow, a base class of TMyTree to which you have a pointer; you
implement the virtual methods for TMyGrow in TMyTree.
Now, TMyGrow::get_IApple() can return the IApple interface of
CLSID_Apple through the IGrow::get_IApple(...) wrapper method. Note
that TMyTree might keep a list of instances of CLSID_Apple that it
creates. For each instance of CLSID_Apple, the local pointer to the
instance of TMyApple can be passed to TMyTree and they can talk
without using the COM interface if they are in the same process space
and are thread safe relative to one another and external users, a
common situation for single-thread apartment (STA) components.
In summary, the pattern is to implement the COM interface requirements
in the COM classes generated by ATL and map them to C++ classes that
do the problem domain work like you would have otherwise. COM takes
care of all the system issues if used correctly. Exactly how to do
this is best determined by engineering your application wisely. I hope
this gives you the general idea of at least one way to do it.
Good luck,
Leo
On Thu, 25 Mar 1999 09:22:32 +1200, "Martin Welford"
Quote
<mart...@trutest.co.nz> wrote:
>Maybe an example will help me understand this!
>If a server has two automation objects defined - ITree and IApple, and ITree
>defines a
>* GrowApple method, and
>* a pointer to an IApple
>Apple has a colour propertry that is set when created by GrowApple, but is
>read only on the IApple interface.
>The client creates a tree object, calls GrowApple, and uses the pointer to
>access the colour of the new Apple object.
>How does the Tree object create the apple and initialise the Colour
>property?
>>The basic problem here is that you are trying to do mix regular C++ class
>>interaction with COM class interaction.
>Do you have to use an interface for all interaction between classes (COM
>classes with COM classes & COM classes with C++ classes) within server?
>Thanks
>Martin.