473,387 Members | 1,863 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,387 software developers and data experts.

Declarations and Definitions in One Header

Now why didn't *_I_* think of that!

Declarations and Definitions in One Header

C++ object definitions can be quite complex. In principle, your source code
will need two kinds of things for each object that you use across more than
one source file. First, you need an interface specification, describing its
structure with type declarations and function prototypes. Second, you need
the implementation itself. It can be tedious to maintain a separate
interface description in a header file, in parallel to the actual
implementation. It is also dangerous, since separate interface and
implementation definitions may not remain parallel.

http://tinyurl.com/2xx3w

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
4 1279

They're called "inline" functions.

Jul 22 '05 #2
Steven T. Hatton wrote:
Now why didn't *_I_* think of that!

Declarations and Definitions in One Header

C++ object definitions can be quite complex. In principle, your source code
will need two kinds of things for each object that you use across more than
one source file. First, you need an interface specification, describing its
structure with type declarations and function prototypes. Second, you need
the implementation itself. It can be tedious to maintain a separate
interface description in a header file, in parallel to the actual
implementation. It is also dangerous, since separate interface and
implementation definitions may not remain parallel.

http://tinyurl.com/2xx3w


This is more of a question of style and preference, rather than a
general rule . As a thumb rule, in C++ - the header files are always
separated from source files (with proper header guards that is, in the
header files).

But then you were right when you mentioned about separating interface
and the implementation. Interestingly, Java does not allow you to do
that. When you define classes, you got to write the define the methods
along with the declarations, there is way we can separate them. ( The
'interface' keyword is entirely different though ).

--
Karthik

------

Human Beings please 'removeme' for my email.
Jul 22 '05 #3
Steven T. Hatton wrote:
Now why didn't *_I_* think of that!

Declarations and Definitions in One Header

C++ object definitions can be quite complex. In principle, your source
code will need two kinds of things for each object that you use across
more than one source file. First, you need an interface specification,
describing its structure with type declarations and function
prototypes. Second, you need the implementation itself. It can be
tedious to maintain a separate interface description in a header file,
in parallel to the actual implementation.
Actually, I see an advantage in doing that. It's cleaner to separate the
two, IMHO.
It is also dangerous, since separate interface and implementation
definitions may not remain parallel.


Why would that be dangerous? If they don't match, the compiler will tell
you so with an error message.

Jul 22 '05 #4
Karthik wrote:
Steven T. Hatton wrote:
Now why didn't *_I_* think of that! http://tinyurl.com/2xx3w

This is more of a question of style and preference, rather than a
general rule . As a thumb rule, in C++ - the header files are always
separated from source files (with proper header guards that is, in the
header files).
I hope you clicked the link. I should have put quotation marks in the
paragraph. It's from the latest GNU gcc documentation. I didn't write it.
But then you were right when you mentioned about separating interface
and the implementation. Interestingly, Java does not allow you to do
that. When you define classes, you got to write the define the methods
along with the declarations, there is way we can separate them. ( The
'interface' keyword is entirely different though ).


You can separate interface from implementation in Java. AAMOF the official
Java recommendation is this: "Every major class in an application should be
an implementation of some interface that captures the contract of that
class." TJPL(3E) page 105. There are also other strategies such as that
used with JAXB. I find that approach to be far more trouble than it's
worth. For the most part, I believe a properly designed class will provide
its own interface without the need to resort to abstracting it.

There is an advantage to implementing a standardized API in the abstract,
and having various implementations created to back it up.

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #5

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

Similar topics

10
by: Alan Lee | last post by:
Hi i am writing a small simulation for a bunch of atoms jumping around on a surface. I know i am a crappy programmer since i am not very familiar with object oriented languages. I am woindering...
4
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...
2
by: Chris Gordon-Smith | last post by:
I am currently in India and have treated myself to the Indian reprint of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to 1/2 of the price in Britain.) I thought that I would...
10
by: Xiaoshen Li | last post by:
Dear All, I am confused with prototypes in C. I saw the following code in a C book: void init_array_1(int data) { /* some code here */ }
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
3
by: MIUSS | last post by:
Hi! I am modifying some part of source code and I now I am suspended by one thing I really don't understand and I got no one experience with it. I got fully working source code (it reports no...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
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,...
0
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...
0
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,...

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.