473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

C++ interface

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.
Jul 22 '05 #1
10 4429

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


Sorry, you get the OOP stuff whether you want it or not. However,
C++ is very efficient about not wasting time on things that are not used.
If your class has no data members or base classes and no user defined
constructor, then I've never seen a compiler that generates any code
on construction or destruction.

Hence a C++ interface is just a class that has:

No non-static data members.
Pure virtual methods for the interface routines.
No base classes that aren't themselves interfaces.

Jul 22 '05 #2
The Directive wrote:
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.

Determine what operations you want to be available as part of this
interface. Implement those operations for all types that must support
the interface. If some code needs to be able to work with anything that
supports the interface, put the code in a template.

template< typename T >
void do_stuff( T const& t )
{
// T supports interface I, which means it's
// ok to perform operations x, y, and z on t.

t.x( );
t.y( );
z( x );
}

This approach is often called "generic programming," although I believe
mathematicians call it "formalism." For a great example of a practical
application of this technique, look at the STL iterators, algorithms and
containers.

-Jef

Jul 22 '05 #3
The Directive wrote:
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.


A good optimizing C++ compiler should not add any overhead.
Can you show us an example where your C++ compiler adds such overhead?

Jul 22 '05 #4
"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/
Jul 22 '05 #5

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:w7******************@twister.nyroc.rr.com...
I know you said you wanted no destructor, but it is really sort of required
for this type of application.


It's only required if you call delete on an interface class pointer. Probably
better to make the destructor protected (and not virtual).

Jul 22 '05 #6
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:w7******************@twister.nyroc.rr.com...
I know you said you wanted no destructor, but it is really sort of required for this type of application.
It's only required if you call delete on an interface class pointer.

Probably better to make the destructor protected (and not virtual).


Sure, but when you write a class like this how are you supposed to know how
derived classes are going to be used? Not including a virtual destructor in
an interface class is unsound IMHO.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:L4*******************@twister.nyroc.rr.com...

It's only required if you call delete on an interface class pointer.

Probably
better to make the destructor protected (and not virtual).


Sure, but when you write a class like this how are you supposed to know how
derived classes are going to be used? Not including a virtual destructor in
an interface class is unsound IMHO.


It's not UNSOUND. It doesn't make a fleas ass difference whether
the interface is derived from or not (and since the class is full of pure
virtual functions it's almost a certain).

By making the destructor PROTECTED as I stated, there's no way to
destroy the class through the interface pointer, which would be the only
time that the destructor would need to be virtual. It was my argument
that it's probably a bad idea to allow people to do this:

Interface* ip = GetMeSomeThingThatSupportsInterface();
delete ip;

Making the destructor protected prevents the above. Making it
virtual does not.

Jul 22 '05 #8
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message

news:L4*******************@twister.nyroc.rr.com...

It's only required if you call delete on an interface class pointer.

Probably
better to make the destructor protected (and not virtual).


Sure, but when you write a class like this how are you supposed to know how derived classes are going to be used? Not including a virtual destructor in an interface class is unsound IMHO.


It's not UNSOUND. It doesn't make a fleas ass difference whether
the interface is derived from or not (and since the class is full of pure
virtual functions it's almost a certain).

By making the destructor PROTECTED as I stated, there's no way to
destroy the class through the interface pointer, which would be the only
time that the destructor would need to be virtual. It was my argument
that it's probably a bad idea to allow people to do this:

Interface* ip = GetMeSomeThingThatSupportsInterface();
delete ip;

Making the destructor protected prevents the above. Making it
virtual does not.


I don't understand your example. If GetMeSomeThingThatSupportsInterface uses
operator new() to create an instance of something derived from Interface
then how does it get deleted? I can't delete it myself without dynamic
casting to a type which has a destructor.

I disagree that deleting an object polymorphically is a bad idea. In fact
making the destructor protected prevents me from using it with smart
pointers.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #9

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:zl*******************@twister.nyroc.rr.com...
I don't understand your example. If GetMeSomeThingThatSupportsInterface uses
operator new() to create an instance of something derived from Interface
then how does it get deleted?
Through some interface call or some external scheme. An example is the Microsoft
COM objects. The IUnkown interface provides reference counting that manages
when the object actually needs to committ suicide. You don't delete stuff through
interfaces in general.
I can't delete it myself without dynamic casting to a type which has a destructor.
Nothing can prevent people being stupid with dynamic cast.
I disagree that deleting an object polymorphically is a bad idea. In fact
making the destructor protected prevents me from using it with smart
pointers.


Nothing precludes that, and it defeats the interface paradigm if you actually
think the interface IS the object. The interface is just a set of manipulations
of the object. Not all interfaces are BASE CLASSES. Some are mixins.

Jul 22 '05 #10
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:zl*******************@twister.nyroc.rr.com...
I don't understand your example. If GetMeSomeThingThatSupportsInterface uses operator new() to create an instance of something derived from Interface
then how does it get deleted?
Through some interface call or some external scheme. An example is the

Microsoft COM objects. The IUnkown interface provides reference counting that manages when the object actually needs to committ suicide. You don't delete stuff through interfaces in general.
Actually, I DO delete things through interfaces! There is an advantage to
it. For instance with COM there is a reference counting mechanism built into
every COM object as you say. But a reference counted smart pointer abstracts
a similar mechanism and keeps it separate from the object itself. This
allows me to write a whole library of polymorphic objects without even
considering reference counting or memory management. All I need to do is
provide a do-nothing virtual destructor in all interfaces.
I can't delete it myself without dynamic casting to a type which has a destructor.

Nothing can prevent people being stupid with dynamic cast.
I disagree that deleting an object polymorphically is a bad idea. In
fact making the destructor protected prevents me from using it with smart
pointers.


Nothing precludes that,


Nothing precludes what? A reference counted smart pointer deletes the object
through the pointer when the smart pointer is destroyed. Hence if the object
cannot be deleted this way the code will not compile.
and it defeats the interface paradigm if you actually
think the interface IS the object. The interface is just a set of manipulations of the object. Not all interfaces are BASE CLASSES. Some are mixins.


An interface with a virtual destructor can be used as a base class or added
later as a "mixin". For instance, in

http://home.rochester.rr.com/cyhome/uvs/samp.html

uvs::IDiDist (an interface to a discrete distribution) is used as a base
class for various discrete distributions but is added to uvs::DiSummary
("mixed in") at the point where we define histogram uvs::DiHistogram. That
just models the fact that a histogram is a distribution and a sample
statistic both.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
2
by: Alex Sedow | last post by:
Why interface-event-declaration does not support multiple declarators like event-declaration? Grammar from C# spec: variable-declarators: variable-declarator variable-declarators ","...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.