Re: C++ interface
From: Cy Edmunds (cedmunds_at_spamless.rochester.rr.com)
Date: 12/24/03
- Next message: Cy Edmunds: "Re: C++ template (standard template class)"
- Previous message: Dave: "Re: Intel c++ 8.0: how to build .dlls and .libs??"
- In reply to: The Directive: "C++ interface"
- Next in thread: Ron Natalie: "Re: C++ interface"
- Reply: Ron Natalie: "Re: C++ interface"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: Cy Edmunds: "Re: C++ template (standard template class)"
- Previous message: Dave: "Re: Intel c++ 8.0: how to build .dlls and .libs??"
- In reply to: The Directive: "C++ interface"
- Next in thread: Ron Natalie: "Re: C++ interface"
- Reply: Ron Natalie: "Re: C++ interface"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|