Declarations and Definitions in One Header

  • Follow


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
0
Reply susudata (549) 4/24/2004 11:56:09 PM

They're called "inline" functions.

0
Reply null25 (1553) 4/25/2004 8:17:46 AM


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.
0
Reply removeme_kaykaydreamz (133) 4/25/2004 8:28:34 AM

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.

0
Reply ramagnus (3485) 4/25/2004 8:41:57 AM

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
0
Reply susudata (549) 4/25/2004 10:24:41 AM

4 Replies
16 Views

(page loaded in 0.067 seconds)

Similiar Articles:













7/14/2012 7:12:46 AM


Reply: