Re: C++ interface

From: Cy Edmunds (cedmunds_at_spamless.rochester.rr.com)
Date: 12/24/03


Date: Wed, 24 Dec 2003 00:31:24 GMT


"The Directive" <the_directive@hotmail.com> wrote in message
news:8477bc58.0312230911.52489402@posting.google.com...
> Is there a way in C++ to create a "true" interface. I want to create
> an absract class that other classes can inherit from and be forced to
> implement the interface's abstract (pure virtual) methods. However, it
> has to be a true interface in that it doesn't add any overhead (when
> the derived object is created) with the interface's constructors and
> destructor because they don't exist for the interface. Does that make
> sense? I just want an interface, not all the OOP stuff like
> constructors, destructor and etc. In other words, I don't want the
> interface's (system default or user defined) constructors & destructor
> to be called.

Here is an example from the uvs library:

class IDiDist // discrete statistical distribution
{
public:
 virtual double
 mean() const = 0;

 virtual double
 variance() const = 0;

 // other pure virtual functions omitted for brevity

 virtual
 ~IDiDist() {}
};

Characteristics of this type of interface:

1) no data
2) no constructor
3) no methods except pure virtual ones
4) a do-nothing virtual destructor

I know you said you wanted no destructor, but it is really sort of required
for this type of application. Otherwise there are circumstances in which a
destructor in a derived class might not get called resulting in a resource
leak. If you can somehow be sure no derived class will have a destructor
(but how?) then you could in principle leave the virtual destructor out.

I generally avoid deriving one interface from another. That's not a hard and
fast rule but seems to work out better. Having a class implement multiple
interfaces in parallel is of course fine.

-- 
Cy
http://home.rochester.rr.com/cyhome/


Relevant Pages

  • Re: C++ interface
    ... > constructors, destructor and etc. ... Pure virtual methods for the interface routines. ... No base classes that aren't themselves interfaces. ...
    (comp.lang.cpp)
  • Re: Dispose, Dispose(true), Finalize, IDisposable
    ... the Destructor such as ... There is no extra code executed when an object goes out of scope, as there is in a system using reference counting, so there is no way that you can make anything happen when an object goes out of scope. ... There is nothing magic about the IDisposable interface, it's just an interface as any other. ... system should still have the responsibility to clean up. ...
    (microsoft.public.dotnet.general)
  • Re: Derived destructor not being called
    ... I've been working on an IDL implementention and I'm having problems ... interface IMyDerived: IMyBase { ... // I'm hoping that the destructor in CMyDerived will be called ... keep un-addref'd pointers to COM objects in containers, ...
    (microsoft.public.vc.language)
  • C++ interface
    ... Is there a way in C++ to create a "true" interface. ... an absract class that other classes can inherit from and be forced to ... destructor because they don't exist for the interface. ... constructors, destructor and etc. ...
    (comp.lang.c)
  • C++ interface
    ... Is there a way in C++ to create a "true" interface. ... an absract class that other classes can inherit from and be forced to ... destructor because they don't exist for the interface. ... constructors, destructor and etc. ...
    (comp.lang.cpp)