"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
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/