What goes where in which files

M

Michael

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
 
D

Daniel T.

Michael said:
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.
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top