COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Declarations and Definitions in One Header

• Email
• 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 (551) 4/24/2004 11:56:09 PM

See related articles to this posting

They're called "inline" functions.


 0
Reply null25 (1555) 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

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

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 (3487) 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

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 (551) 4/25/2004 10:24:41 AM

4 Replies
28 Views

Similar Articles

12/11/2013 1:09:58 PM
[PageSpeed]

Similar Artilces:

Inheritance, Class declaration and header files
This is the problem: I have to inherit a third party class file in my library. If I directly do it, I will have to ship the third party header files to my customers as well. I do not want to ship the third party header files to my customers. What is the most elegant way to handle this problem. For example: // MyClass.h #include "ThirdPartyClass.h" class MyClass : public ThirdPartyClass { }; I would prefer keeping my class definitions in the header files. Thanks Sim "Sim Smith" <yesbkay@yahoo.com> wrote in message news:b2a59a3b.0409252042.57191042@posting.googl...

Declaration, Definition & Reference listers for C
I'm interested in the large scale structure of very large programs (1000's of files) So to experiment with this I need to extract every {typedef, struct, union, enum, macro, variable and function} {declaration, definition and reference} from 1000's of C files. Anybody know of a tool to parse standard C and spit out this information? I would write one myself, but to do it properly I would have to write a C preprocessor and most of C compiler. -- John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO ...

How to declare interdependent classes in different header files
Is it possible to declare interdependent classes in different header files? If so, how could the following errors in the following piece of code be corrected? Thanks in advance, Andre 1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C2146: syntax error : missing ';' before identifier 'mStructA' 1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430: missing type specifier - int assum...

declare a struct with c++ vectors in a header file...
Hi all, I tried to declare a c++ struct like following in a header file: ------ 1 #ifndef _SEARCHDATA_H_ 2 #define _SEARCHDATA_H_ 3 4 #include <vector> 5 #include <list> 6 7 typedef struct searchIterInfo searchIter; 8 struct searchIterInfo { 9 searchIterInfo (): 10 contPts(0), //continuous trial points 11 contFvs(0), //fv for cont points 12 contFerrors(0), //std errors for cont points 13 gradients(0), //list of gradients 14 intPts(0), //evaluated int poi...

"unable to match function definition to an existing declaration"
Hi. When I try to compile this piece of Code with VC.NET 2003 // code start #include <vector> template< class T > struct Marker { public: Marker() : err( 0 ) {} unsigned long err; }; template< class T > struct MarkerTable { public: typedef std::vector< Marker<T> > TableType; TableType data; typename TableType::iterator FindErr( unsigned long ); // line 15 }; template< class T > typename MarkerTable<T>::TableType::iterator MarkerTable<T>::FindErr( unsigned long n ) { typename TableType::iterator iter ...

g009: The header file (for a class) shall contain the class declaration
This is another of those guidelines that I consider to be self-evident. I have posted such guidelines recently and weaknesses/errors have been shown to be by others. So you never know. Perhaps there are issues I haven't thought of. Comments please. GUIDELINE ========= The header file shall contain the class declaration. REASON ====== This should describe the class fully, so that a user of the class only needs to look here to see how it may be used. The user should never have to look in the source file, due to the fact that the class should be viewed as a high level abstraction with no n...

Why this doesn't violate One definition rule and how the linker resolves this
I am using gcc compiler gcc B.o Main.o -lMyLib //A.h class A { public: A(); void abc (); void bcd (); }; Class A is defined in MyLib ie (A.cpp) is in MyLib and it also contains A.h Main.cpp also #include "A.h" Now if change the A.h #included in Main.cpp only as //A.h changed only in Main.cpp //A.h class A { public: A(); void abc (); void bcd (); Test () {std::cout << "hello world " << std::endl; } }; This still compiles and I can invoke the Test in Main.cpp. This compiles even if Test is non-inline. Does n...

class declarations and using declarations
I've ran into an issue in trying to use a using declaration for versioning, similar to the technique Stroustrup recommends on page 409 of D&E. Here's the code that can cause this. class Foo; namespace v1 { class Foo {}; } namespace v2 { class Foo {}; } using v1::Foo; // error redeclaration of 'Foo' I understand why this is an error, but I don't understand the reason for this in the language. (Note MSVC 7.1 reports this error incorrectly as a redeclaration of 'v1::Foo', however, comeau has it right in saying the error is a redeclaration of 'Fo...

hi all, i have a problem of declaring&defining 2 classes which contain one another in the classes' attribute as illustrated below: class A { public: B* ptrB; } class B { public: A* ptrA; } i tried several ways, but not still not able to compile correctly: 1. when they are in 1 same file, the compiler complaint that it doesn't recognise class B in class A declaration. 2. i separate the 2 classes in 2 files, then include each other as a header, but then i get this error message: warning C4182: #include nesting level is 363 deep; possible infinite recursion fatal error C1076:...

class declarations in function declarations
Hallo, I was shown the following syntax to overcome circular dependency problems: struct A { int f(class B b); } struct B {}; I tried it out and discovered that the Comeau compiler supports it. Is it valid? If it is, could someone please point me to where it is defined in the standard? I tried to find it but didn't succeed. Thank you, Nicola Musatti [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] Nicola Musatti ha scritto: > struct A { > int f(class B b); > } > > struct B {}; &...

I wish to put only few word to the header; no page numbers, no line drawn below the header. I tried the fancy header package but could not manage to get rid of the line and the page numbers. Can you tell me how I can simply put some words on the first page of my document without any other fancy stuff surrounding. minimus a �crit : > I wish to put only few word to the header; no page numbers, no line drawn > below the header. I tried the fancy header package but could not manage to > get rid of the line and the page numbers. Can you tell me how I can simply > put some wor...

One-on-one training for MacSpeech Dictate
One-on-one training is available for MacSpeech Dictate, it started December 1, 2009. <http://www.macspeech.com/article_info.php?articles_id=352> I have not had a chance to check it out yet, however I will, and I will report back to advise whether or not it is worth the tuition charge. To my opinionated way of thinking, the ordinary user is better off learning the hard way, as I did; one problem with that learn-by-yourself method is that users tend to become discouraged, then drop all interest in speech recognition. Mark- In article <101220091208084302%aeiou...

How might a provider pull together better, easier ways to explain, instruct a. using less of what might be perceived as jargon and b. using less what might be perceived as arcane references, how might a provider more easily explain, more easily instruct end users with these questions?... end users with areas of expertise not directly applicable to... 1. What can you do with the spamassassin headers?... spamassassin headers have appeared in the headers at the top of messages in emacs rmail. 2. And how do you do the things you can do with the spamassassin headers?... Around the web the instru...

Picking Element from Array one by one
Hi All, I am getting some values from loop and i store all that value in array using push function. push @start,$start2; push @end,$end2; the start values are 68 801 968 40 and the End values are 320 830 995 56 then I sort values of array- my @sort_start = sort { $a<=>$b } @start; my @sort_end = sort { $a<=>$b } @end; print @sort_start,"\n"; print @sort_end,"\n"; I am getting output 4068801968 56320830995 Is there any way that i can get output like this- start -40 End - 56 start -68 End- 320 start-801 End -830 start-968 End-995 I cann't able to figu...

[LogoForum] One-to-one computing for under \$100
--Apple-Mail-18-458018690 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed http://www.eschoolnews.com/news/showStoryts.cfm?ArticleID=6591 __._,_.___ LogoForum messages are archived at: http://groups.yahoo.com/group/LogoForum --Apple-Mail-18-458018690 Content-Transfer-Encoding: 7bit Content-Type: text/enriched; charset=US-ASCII <underline><color><param>1999,1999,FFFF</param>http://www.eschoolnews.com/news/showStoryts.cfm?ArticleID=6591 </color></underline>__._,_.___ LogoForum messages are archived at: <...

poster works one one machine but not on other
Hi I am somewhat clueless with respect to (at least ;-) ) the following problem: I did a poster w/ a0poster and everything works fine on my home PC but not on my office PC. Both have miktex 2.4 and missing packages on the latter can hardly be the issue (as evidenced by, for instance, the fact that previous posters worked fine on both). I don't see any pecularities in the current poster. The error message on the office PC is "not in outer par mode" before \begin{figure}, and a hopefully acceptably minimal example goes as follows: Thank you! \documentclass[a0klein,portrait]{a0p...

one-liner to make all programs one-liners
>From deep in my secret lab, I have reduced millions of lines of code to a single line with this => s/\n//g and print while <>; Be very careful... On 3 Feb 2005 14:51:41 -0800, Larry <larry_wallet@yahoo.com> wrote: >>From deep in my secret lab, I have reduced millions of lines of code to > a single line with this => > > s/\n//g and print while <>; > > Be very careful... Indeed. Try this one: --- begin --- #!/usr/local/bin/perl print "Hello World!\n" --- end --- And if you insists that the shebang line isn't part of the pro...

chained comparsion
Imagine you need to write a comparsion function int compare(Foo const& a, Foo const& b) or bool operator<(Foo const& a, Foo const& b) implementing a lexicographical order on a set of Foo's members. And you would like to do this as clear as possible: you hate multiple exits, gotos and macros. Thus, a common way bool operator<(Foo const& a, Foo const& b) { if(a.x < b.x) return true; if(b.x < a.x) return false; if(a.y < b.y) return true; if(b.y < a.y) return false; // et cetera return false; } is rejected. What could you ...

why arent C++ exceptions declared in function declaration?
Hello, In Java, if a function body has a throw(SomeException) statement, then, this must be explicitly declared in the function declaration via a throws SomeException clause. (yes yes as long as SomeExceptions is a descendant of Throwable or something like that) In C++ the throws declaration is optional. I would like to know why this is so. Is it just a stylistic issue or are there deeper reasons related to the C++ programming model? Thanks! [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] On 22 Dec ...