C
christopher diggins
The following is a post I recently made to comp.std.c++ that I wanted to
discuss here, due to the horrible lag when trying to have a discussion on
the moderated groups. I would appreciate some feedback on the factual
content, and likelihood of acceptance. Thanks in advance!
--
Motivation
==========
Interface constructs as found in languages like Java are used to model
looks-like and behaves-like object relationships. C++ does not support
interfaces.
Common Workaround
=================
What is typically used to compensate for this deficiency are abstract base
class (classes with one or more pure virtual functions).
Deficiency of Workaround
========================
The problem of using an ABC as an interface is that it declares the
functions virtual. This is uneccessary and in fact wrong with regards to
most definitions of an interface. This leads to two major practical
problems: performance penalties due to extra vtable lookups and object size
penalties due to extra vtable pointers within the objects. This is
especially troublesome because design models that use interfaces typically
call for multiple interfaces, thus compounding the penalties, and making
many perfectly acceptable designs unusable in practice. (for an example of
the signficant performance penalty of ABC's versus interfaces using a
popular C++ compiler see http://www.heron-language.com/hfront-test.html )
Proposed Solution
=================
Allow declaration of an interface, like a class/struct but with only
function declarations, i.e.:
interface IFuBar {
FuBar();
};
Allow declaration of intent of implementation by a class, i.e.:
class CFuBar {
implements: IFuBar;
};
Allow variables to be declared of type interface reference type i.e.:
CFuBar x;
IFuBar i = &x;
Interface reference types can be constructed and assigned from any object
that implements the named interface, or from any other like-typed interface
reference. Interface references would provide a set of member functions
equivalent to the functions listed in the interface declaration. Interface
references are assignable with the NULL value, and are comparable to NULL.
Proposed Implementation
=======================
An effective implementation of interface references is to use fat pointers
that refer to an object and a function lookup table. The fat pointer
solution implementation compares favourably with ABC's in many scenarios. An
example implemenation that uses the fat pointer approach has been posted at
http://www.heron-language.com/heronfront.html .
Possible Syntax Changes
=======================
Diggins syntax:
interface FuBar { ... }; // to indicate interface declaration
class CFuBar { implements: FuBar; ... }; // indicates class implementing
interface
Barbati syntax (variant 1):
class IFuBar = 0 { ... }; // to indicate interface declaration
class CFuBar : IFuBar { ... }; // indicates class implementing interface
Barbati syntax (variant 2):
virtual class FuBar { ... }; to indicate interface declaration
class CFuBar : IFuBar { ... }; // indicates class implementing interface
Thanks to Alberto Barbati for his syntax proposals.
Repercussion of Proposal
========================
There are no repercussions to the language above and beyond the syntactic
changes required to allow declaration of interfaces and implementation of
interface.
discuss here, due to the horrible lag when trying to have a discussion on
the moderated groups. I would appreciate some feedback on the factual
content, and likelihood of acceptance. Thanks in advance!
--
Motivation
==========
Interface constructs as found in languages like Java are used to model
looks-like and behaves-like object relationships. C++ does not support
interfaces.
Common Workaround
=================
What is typically used to compensate for this deficiency are abstract base
class (classes with one or more pure virtual functions).
Deficiency of Workaround
========================
The problem of using an ABC as an interface is that it declares the
functions virtual. This is uneccessary and in fact wrong with regards to
most definitions of an interface. This leads to two major practical
problems: performance penalties due to extra vtable lookups and object size
penalties due to extra vtable pointers within the objects. This is
especially troublesome because design models that use interfaces typically
call for multiple interfaces, thus compounding the penalties, and making
many perfectly acceptable designs unusable in practice. (for an example of
the signficant performance penalty of ABC's versus interfaces using a
popular C++ compiler see http://www.heron-language.com/hfront-test.html )
Proposed Solution
=================
Allow declaration of an interface, like a class/struct but with only
function declarations, i.e.:
interface IFuBar {
FuBar();
};
Allow declaration of intent of implementation by a class, i.e.:
class CFuBar {
implements: IFuBar;
};
Allow variables to be declared of type interface reference type i.e.:
CFuBar x;
IFuBar i = &x;
Interface reference types can be constructed and assigned from any object
that implements the named interface, or from any other like-typed interface
reference. Interface references would provide a set of member functions
equivalent to the functions listed in the interface declaration. Interface
references are assignable with the NULL value, and are comparable to NULL.
Proposed Implementation
=======================
An effective implementation of interface references is to use fat pointers
that refer to an object and a function lookup table. The fat pointer
solution implementation compares favourably with ABC's in many scenarios. An
example implemenation that uses the fat pointer approach has been posted at
http://www.heron-language.com/heronfront.html .
Possible Syntax Changes
=======================
Diggins syntax:
interface FuBar { ... }; // to indicate interface declaration
class CFuBar { implements: FuBar; ... }; // indicates class implementing
interface
Barbati syntax (variant 1):
class IFuBar = 0 { ... }; // to indicate interface declaration
class CFuBar : IFuBar { ... }; // indicates class implementing interface
Barbati syntax (variant 2):
virtual class FuBar { ... }; to indicate interface declaration
class CFuBar : IFuBar { ... }; // indicates class implementing interface
Thanks to Alberto Barbati for his syntax proposals.
Repercussion of Proposal
========================
There are no repercussions to the language above and beyond the syntactic
changes required to allow declaration of interfaces and implementation of
interface.