Re: interface programming



dummy#1 wrote:
Hi All,

I have a question.

In C#, I have auto garbage collector so I can freely do:

ICar pGenericCar = carFactory.createCar( toyotaEnum );

Without worrying about memory leaks.

in C++, how can I do the same without memory leaks?

ICar pGenericCar = carFactory.createCar( toyotaEnum ); // who will
delete this heap object since C++ interfaces have no ctor or dtor?

There's no such thing as an interface in C++ - there are classes and
structs, that's all. Unless you're talking about managed C++ that is, in
which case you also have garbage collection so there's nothing to worry
about.

For native C++, there are two common patterns for interfaces:

1. COM style - AddRef/Release used to manage object lifetime.
2. Virtual destructor - object holder manages lifetime.

Which kind of "interface" are you working with?

-cd


.



Relevant Pages

  • Re: interface programming
    ... ICar pGenericCar = carFactory.createCar; ... Without worrying about memory leaks. ... delete this heap object since C++ interfaces have no ctor or dtor? ... the garbage collector will work exactly yhe same as in C#. ...
    (microsoft.public.vc.language)
  • Re: New language feature in Delphi: Static instantiation of classes
    ... interfaces are created somewhere and not just appear magically ... I would understand garbage collector idea, but not such free fly like ... how would this obsfucate the code anymore than the use of interfaces? ...
    (borland.public.delphi.non-technical)
  • Re: Dream Controls: TDcInfoTree
    ... probably from circular references. ... I could accept some memory leaks, but what I can't accept in production ... code is a ticking time bomb: ... Release calls, mixing reference variable and interfaces, converting ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Designfrage - Klassen vs. Records
    ... Der Code wird durch viele try-Blöcke zerklüftet und das Risiko von Memory Leaks steigt. ... Und mehr Kontrolle über Abhängigkeiten zwischen Typen, da nur Methoden und Properties der Interfaces verwendet werden können. ...
    (de.comp.lang.delphi.misc)