473,655 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do we have to manually separate interface and implementation?

Hello World,

This is not a flame, but a question about the fundamentals of the
language. Unlike some languages, C++ requires class member functions
to be declared twice: once in the class declaration and again in the
definition. This is not too much of a pain for most simple classes but
I really hate the syntax for member functions of templated classes:

template<class A, class B, class C>
void ClassName<A, B, C>::FuncName() {}

You have to either do that for every method, or define all the methods
inline (looking at the MS STL implementation, it looks like they chose
the latter option even for some large functions).

Also, features like inline functions, const, and namespaces complicate
the issue further. My question is is there really an overall benefit
in having to do this, rather than have the interface extracted
automatically (eg. as in C# and Java). Is this necessity in C++
because of it's inheritance from C, or would the designers have put in
a proper module system if they were designing the language from
scratch?

- Paul

Jul 22 '05 #1
6 3508
Paul Fame wrote:
Hello World,

This is not a flame, but a question about the fundamentals of the
language. Unlike some languages, C++ requires class member functions
to be declared twice: once in the class declaration and again in the
definition. This is not too much of a pain for most simple classes but
I really hate the syntax for member functions of templated classes:

template<class A, class B, class C>
void ClassName<A, B, C>::FuncName() {}

You have to either do that for every method, or define all the methods
inline (looking at the MS STL implementation, it looks like they chose
the latter option even for some large functions).

Also, features like inline functions, const, and namespaces complicate
the issue further. My question is is there really an overall benefit
in having to do this, rather than have the interface extracted
automatically (eg. as in C# and Java).
I don't know much about C#, but the interfaces are *not* extracted
automatically in Java.
Is this necessity in C++
because of it's inheritance from C,
No, it was added to benefit developers. In the version of C that
existed when C++ was first being written, you could use a function
without ever declaring it. The compiler will just assume the function
is defined in another module.
or would the designers have put in
a proper module system
In what way is a "proper module system" lacking?
if they were designing the language from scratch?


I highly doubt it, but that would be a good question for comp.lang.c++.

Keeping the interface separate from the implementation allows you to
concentrate on one, without regard for the other. C++ header files are
not just an implementation detail; they are a place to document your
module's interface.

If the typing really bothers you, I'm sure you could find a tool to
generate the interface files for you (something akin to javadoc). A
first pass search found this:

http://freshmeat.net/projects/makehe...ic_id=259%2C45

It looks a little outdated, but that's probably evidence of how little
it is needed.

Hth,
Jeff

Jul 22 '05 #2
Jeff Schwab wrote:
Paul Fame wrote:
Hello World,

This is not a flame, but a question about the fundamentals of the
language. Unlike some languages, C++ requires class member functions
to be declared twice: once in the class declaration and again in the
definition. This is not too much of a pain for most simple classes but
I really hate the syntax for member functions of templated classes:

template<class A, class B, class C>
void ClassName<A, B, C>::FuncName() {}

You have to either do that for every method, or define all the methods
inline (looking at the MS STL implementation, it looks like they chose
the latter option even for some large functions).

Also, features like inline functions, const, and namespaces complicate
the issue further. My question is is there really an overall benefit
in having to do this, rather than have the interface extracted
automatically (eg. as in C# and Java).

I don't know much about C#, but the interfaces are *not* extracted
automatically in Java.
Is this necessity in C++
because of it's inheritance from C,

No, it was added to benefit developers. In the version of C that
existed when C++ was first being written, you could use a function
without ever declaring it. The compiler would just assume the function
was defined in another module.
or would the designers have put in
a proper module system

In what way is a "proper module system" lacking?
if they were designing the language from scratch?

I highly doubt it, but that would be a good question for comp.lang.c++.


:%s/lang/std/g

Keeping the interface separate from the implementation allows you to
concentrate on one, without regard for the other. C++ header files are
not just an implementation detail; they are a place to document your
module's interface.

If the typing really bothers you, I'm sure you could find a tool to
generate the interface files for you (something akin to javadoc). A
first pass search found this:

http://freshmeat.net/projects/makehe...ic_id=259%2C45
It looks a little outdated, but that's probably evidence of how little
it is needed.

Hth,
Jeff


Jul 22 '05 #3
Hi,
I don't know much about C#, but the interfaces are *not* extracted
automaticall y in Java.
But in Java you don't have to duplicate function declarations and
definitions - you can define all your methods inline. (I dislike Java
compared to C++ for other reasons though).
or would the designers have put in
a proper module system


In what way is a "proper module system" lacking?


Modules are done by textual inclusion. Any macros defined before the
#include can subtly modify the code in the included file. And then
there are the #pragma once or include guard kludges. Modules done by
textual inclusion often mean slower compiles than would be possible if
C++ had a proper module system.
Keeping the interface separate from the implementation allows you to
concentrate on one, without regard for the other. C++ header files are
not just an implementation detail; they are a place to document your
module's interface.


Glancing over the MS STL, the boost libraries, and several other
proprietary libraries, it seems they view header files as places to
put code when they couldn't be bothered typing it out in a separate
file. If you don't have a compiler that ignores this "inline"
suggestion, you will also find your object code bloated.

The C++ method is surely better than the C method of no argument
checking. Obviously it is too late for a change now... but I still
think a lot of the perceived complexity of C++ would disappear if you
didn't have to do this manual separation of interface and
implementation. Having worked in both Java and C++ for a while, I find
the former's method to be easier on code creation and maintenance.

- Paul

Jul 22 '05 #4
Paul Fame wrote:
But in Java you don't have to duplicate function declarations and
definitions - you can define all your methods inline. (I dislike Java
compared to C++ for other reasons though).

You can do the same thing in C++.

Modules are done by textual inclusion. Any macros defined before the
#include can subtly modify the code in the included file. And then
there are the #pragma once or include guard kludges. Modules done by
textual inclusion often mean slower compiles than would be possible if
C++ had a proper module system.

Thanks, now I see what you mean. The "include guards" really are a
kludge. According to Stroustrup, though, those are issues outside the
scope of the language definition. He predicted in _Design_&_Evolu tion_
(1994) that development environments would get much better, and that the
currently popular system of file inclusions eventually might go out of
vogue altogether. To some extent, he was right; the performance hit
that comes from textual inclusion is getting much less significant as
pre-compiled header compilation gains popularity. The Sun compiler has
been particularly good about this, and about sharing templates' object
code. Java handles much of this internally, in the same way that it
doubles as Make for small programs. It's really just a matter of where
you draw the line around what is (not) part of the language. Java is
clearly meant to be an entire platform; C++ is not.
Glancing over the MS STL, the boost libraries, and several other
proprietary libraries, it seems they view header files as places to
put code when they couldn't be bothered typing it out in a separate
file. If you don't have a compiler that ignores this "inline"
suggestion, you will also find your object code bloated.

I don't think those functions were inlined out of laziness. Actually,
that's a pretty funny (and somewhat insulting) assumption. :)

A huge chunk of the standard library is based on templates that must be
inlined. The fact is that the code bloat really isn't that bad. Of
course, there are systems that will page fault like mad on large object
files, but I think you'd have an uphill battle to argue that Java could
be squeezed into tighter spaces that C++.

The C++ method is surely better than the C method of no argument
checking.

That's the old C method. I believe C99 does require function
declarations with appropriate argument lists ( gurus please correct me ).

Obviously it is too late for a change now...

Nonsense. The bits you feel are lacking can always be provided by
compilers as enhancements, and popular enhancements are always great
candidates for additions to the standard. The enhancements have been
slow in coming because dissatisfaction with the current methodology has
been low.

but I still
think a lot of the perceived complexity of C++ would disappear if you
didn't have to do this manual separation of interface and
implementation.

Well, you don't have to do any such thing, but you do have a point.
Perhaps students should first be taught to define all methods inline,
then be taught the more traditional approach as an "advanced technique
for reducing object-file size."

Having worked in both Java and C++ for a while, I find
the former's method to be easier on code creation and maintenance.

Really? I haven't worked extensively with Java, but I have tried to use
it for pet projects. Anyway, I'm glad it works for you; I think the
platform is brilliant. I'd love to see a C++ compiler for the JavaVM,
but I'm not holding my breath.

-Jeff

Jul 22 '05 #5
Hi,
But in Java you don't have to duplicate function declarations and
definitions - you can define all your methods inline. (I dislike Java
compared to C++ for other reasons though).

You can do the same thing in C++.


But your code is then not compiled separately. And if you're unlucky,
your compiler will inline all the functions, causing massive code
bloat.
Thanks, now I see what you mean. The "include guards" really are a
kludge. According to Stroustrup, though, those are issues outside the
scope of the language definition. He predicted in _Design_&_Evolu tion_
(1994) that development environments would get much better, and that the
currently popular system of file inclusions eventually might go out of
vogue altogether. To some extent, he was right; the performance hit
that comes from textual inclusion is getting much less significant as
pre-compiled header compilation gains popularity.
Yeah. What I have been doing in my projects is making a single master
header that includes everything, and having every module include that
header. A strange way to organise software, but with precompiled
headers it makes it compile fastest.

I don't think those functions were inlined out of laziness. Actually,
that's a pretty funny (and somewhat insulting) assumption. :)

But the functions are often pretty large, and shouldn't have been
inlined. The only conclusion I can draw is that they (like me) don't
like the syntax for out-of-line methods of class templates.
A huge chunk of the standard library is based on templates that must be
inlined. The fact is that the code bloat really isn't that bad. Of
course, there are systems that will page fault like mad on large object
files, but I think you'd have an uphill battle to argue that Java could
be squeezed into tighter spaces that C++.


Agreed, Java is a memory and CPU hog. But my standards are high, as I
began programming on the Commodore 64, which fit a BASIC interpreter
into 8K, where every useful game or app was written in pure assembly
language, with dynamic memory a laughable concept. Now it saddens me
that I compile Hello World in MSVC++ and it churns out a 160KB
executable.

- Paul

Jul 22 '05 #6
"Paul Fame" <no****@none.co m> wrote...
But in Java you don't have to duplicate function declarations and
definitions - you can define all your methods inline. (I dislike Java
compared to C++ for other reasons though).

You can do the same thing in C++.


But your code is then not compiled separately. And if you're unlucky,
your compiler will inline all the functions, causing massive code
bloat.


Nonsense. You can use pimpl idiom and compile them separately.

One thing you should probably remember: there is no free lunch. You
either have all your functions in one place or you have lean and very
well organised system.
[...] What I have been doing in my projects is making a single master
header that includes everything, and having every module include that
header. A strange way to organise software, but with precompiled
headers it makes it compile fastest.
Standard C++ does not define anything named "precompile d headers".
Keep that in mind :-)
Agreed, Java is a memory and CPU hog. But my standards are high, as I
began programming on the Commodore 64, which fit a BASIC interpreter
into 8K, where every useful game or app was written in pure assembly
language, with dynamic memory a laughable concept. Now it saddens me
that I compile Hello World in MSVC++ and it churns out a 160KB
executable.


Well, don't blame the language, blame the compiler and library
creators.

See http://www.research.att.com/~bs/bs_faq.html#Hello-world (and
other questions and answers for more information).

Victor
Jul 22 '05 #7

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

Similar topics

4
1464
by: Andreas Schmidt | last post by:
Suppose there is a 10,000 lines header file that wildly mixes definitions (the interface) and implementations. Other code that includes this header file takes hours to compile, and it's impossible to compile a shared library from the header file, of course. Is there a tool that takes this header file, and cuts out the implementations, writing them into a separate .cc file? It would automatically create a clean separation of interface and...
3
2371
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached response). My first reply directed me to source code migration but I didn't have the source code. The second reply mentioned about .NET interoperability (PInvoke I think) but I MENTIONED THAT I COULDN'T FIND ANY DOCUMENTATION FROM MSDN LIBRARY BASED ON...
21
13818
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
4
2844
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
6
1936
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used subroutines, etc. Now I'm trying to study this new way and I'm getting some terms confused and can find no clear definition (some even overlap or use two different words for the same thing, even when they are actually different). I'm reading a...
8
3943
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For example: class A { public: const vector<int>& getTable() { return m_table; }
0
1194
by: Luc Kumps | last post by:
We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this, as we seem to lack only a single 'step' to have "full separation"... We have a first project, namespace P1, that contains the interface definitions, like this:
5
2674
by: Junoti | last post by:
Hello, I'm hoping someone might be able to help me out. I'm creating a shared assembly with my interface definitions similar to the following. When I try to compile the RemotingInterface, everything works. When I go to compile the RemotingServer assembly, I receive the following error message. I've included all my references, so I'm almost positive that's not the issue. Shouldn't this be possible without having to implement HelloMessage in...
52
20869
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.