Re: ATL-COM: return pointer to instance

From: Simon Trew (noneofyour_at_business.guv)
Date: 02/17/05


Date: Thu, 17 Feb 2005 11:40:20 -0000


"Joe" <joerider@uboot.com> wrote in message
news:uumercDFFHA.628@TK2MSFTNGP15.phx.gbl...
>
> If I want to access to a function on CTree/ITree I can redirect it to
> tree_. The problem is, that I don't want to create an Instance of
> CItem/IItem for each Item-Instance. Otherwise I would rebuild the whole
> tree a second time.
>
> In my opinion CItem should hold a pointer to the original Item-Instance.
> So I can access all members directly.

Okay, that's what I was trying to say before. Your CItem is just a proxy for
the 'real' data and you expect it to have a shorter lifetime (i.e. it can be
created/deleted as required, without the 'real' data being created/deleted).
So yes, use a pointer or reference back to the original item. I'd call this
the 'proxy' pattern, though it may well be that there's another formal name
for it (on the whole I find the GoF's pattern names rather cryptic).

> If I want to access the root, I somewhere need to create an instance of
> a CItem and delete it somewhere. Then I want to read some Variables like
> the name and afterwards the object can be destroyed.

> For what reason should I call parent->AddRef() ?
> Does this delete the Object when I set the Object in VB to Nothing?

You should call AddRef any time you are handing a new reference to the
interface. In this case you are handing out parent to the VB client, so now
both your own code and the VB code refer to the interface, so you should
call AddRef before handing it out.

VB will call Release() when you set the Object variable to Nothing. Then,
the stock ATL code for Release() will end up deleting the object (with
"delete this") if the reference count has fallen to zero.

> How can I do that? Another Memberfunction?

You don't need to; the VB client will. All interfaces inherit from IUnknown
which defines AddRef() and Release(), and you will inherit your class from
an ATL class (CComObject) which defines stock implementations.

> Thanks, Joe



Relevant Pages

  • Re: [patch 0/8] Nesting class_device patches that actually work
    ... you give one of your "subclass devices" an interface, ... Take a step back and look what a kernel interface is about. ... device tree by following the "device" symlink from sysfs to get a unified tree ... the hierarchy is not a reason not to be able to walk up the chain and change ...
    (Linux-Kernel)
  • Re: developing for something that isnt there..
    ... xml was chosen becouse well. ... creating a tree model of javabeans was my insisting becouse i didnt' ... had a complelty differnt strucutre and style of interface, ... and interface to send messages about them the classes ...
    (comp.lang.java.programmer)
  • Re: What I dont like about C# so far, compared to C++ (managed or
    ... locking type. ... program "using" the interface is show. ... probably never use it ("The IConfiguration interface defines ... Amazing that my home grown tree is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Open Firmware device tree virtual filesystem
    ... the of_* routines continue to define the ... The main problem with the current powerpc interface to the device-tree ... tree is in kernel memory, and the lifetime of the properties is tied to ... In general, we don't fabricate the @unit-address part, we use OF's own ...
    (Linux-Kernel)