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 6 3494
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
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
Hi, I don't know much about C#, but the interfaces are *not* extracted automatically 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
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_&_Evolution_
(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
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_&_Evolution_ (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
"Paul Fame" <no****@none.com> 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 "precompiled 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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: 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.
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
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...
| |