By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

What goes where in which files

P: n/a
Ok, I'm just checking i'm on the right lines:
sorry if i make a mistake with any strict definitions of things

in a .h , try and put:

class declaration (prototype)
relevant forward declarations for pointers in the class,
#includes required for the objects aggregated in the class
#includes required for base-classes
#includes for templates

in .cpp we put:

class definition
#includes to resolve the forward declarations in the .h if the pointer
declarations are used.
#defines for class

Where do the namespace 'definitions' go, I've been putting them around both
files
Also where to globals go, I would have though the .h with Guard headers?

Thanks Mike


Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Michael" <sl***********@hotmail.com> wrote:
Ok, I'm just checking i'm on the right lines:
sorry if i make a mistake with any strict definitions of things
From "The C++ Programming Language" 3rd Ed by Bjarne Stroustrup

"""
As a rule of thumb, a header may contain:

Named namespaces: namespace N { /*...*/ }
Type definitions: struct Point { intx, y; };
Template declarations: template< typename T > class Z;
Template definitions: template< typename T > class V { /*...* }
Function declarations: extern int strlen( const char* );
Inline function definitions: inline char get(char* p) { return *p++; }
Data declarations: extern int a;
Constant definitions: const float pi = 3.141593;
Enumerations: enum Light { red, yellow, green };
Name declarations: class Matrix;
Include directives: #include <algorithm>
Macro definitions: #define VERSION 12
Conditional compilation directives: #ifdef __cplusplus
Comments: /* check for end of file */

Conversly a header should never contain:

Ordinary function definitions: char get(char* p) { return *p++ };
Data definitions: int a;
Aggregate definitions: short tbl[] = {1, 2, 3};
Unnamed namespaces: namespace { }
Exported template definitions: export template<class T>f(T t){ /*...*/}
"""

I include global using directives and declarations in the "should never
contain" list.

Where do the namespace 'definitions' go, I've been putting them around both
files
I'm not sure what you mean by namespace 'definitions', but the answer
should be in the above.
Also where to globals go, I would have though the .h with Guard headers?


The declaration part of the global goes in the header (and can be in
more than one header as long as they are all the same,) the definition
of the global goes in *one* cpp file.
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.