Declarations and Definitions in One Header

S

Steven T. Hatton

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
 
K

Karthik

Steven said:
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 ).
 
R

Rolf Magnus

Steven said:
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.
 
S

Steven T. Hatton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top