Thread: Makefiles
View Single Post
Old 21st October 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by JMJ_coder View Post
So I should have the class definition in one file (header file) and the class methods in another file (cpp file)?
Definitions imply that memory is being allocated.

Class declarations should be placed into header files, & most member functions should be implemented in a single source code file. The only exception to this convention is inline member functions or inline functions. Here, public symbols are not being generated for the linker to see. inline code is also often treated as code which is substituted elsewhere. Because of this, inline is essentially treated as #define preprocessor commands.

However in C++, note that all inline member functions or member functions defined within the class's declaration (which means that they are to be inlined...) are not always inlined. Simple substitution is left up to compiler vendors to determine whether they can perform the substitution. As a rule of thumb, C++ compilers will not inline any code which implements a branch -- while-loops, for-loops. etc.
Quote:
If that is so, what do I include in the main.cpp file - thing.h; thing.cpp; both; neither?
Including source code files is never, ever done. Never.

Think of the problem involving system headers. For example, the prototype for printf() is found in stdio.h, yet the implementation is found in code which ultimately is compiled & placed into /usr/lib/libc.so. What is required in application code? Mere inclusion of stdio.h is sufficient because all the compiler needs to know is the function's signature. It is the linker's responsibility to resolve all symbols where are present in other object files or libraries.

So to answer the fundamental question, when a source code file containing main() instantiates (allocates memory...) a class, all the compiler needs to see beforehand is the class' declaration. Typically, this means that the header file containing the class declaration was included at the top of the source code file.
Quote:
Also I do have some global function prototypes in the header file, and some defines:

Code:
#include <curses.h>
#include <math.h>

#define POSX 50
#define POSY 20
#define DOORTOP 7
#define DOORBOTTOM 13

/* forward declarations */

class agent;
class dot;
class guard;
class hackbot;
class slashbot;
class thing;

/* function prototypes */

void draw(WINDOW*, thing[], hackbot, slashbot, dot);
bool check_wires(agent, thing[]);
bool collide(agent, agent);

Should these be in a header file?
#define statements are simply instructions to the preprocessor to perform substitutions. Preprocessor commands can safely be placed in header files.

As for the remainder of what is specified above, no memory allocations are being specified (variable definitions) nor is any compilable code provided, so everything above is safe.

So to be clear, the following is all that should ever be in header files:
  • Function prototypes.
  • extern statements.
  • Preprocessor directives.
  • Declaration of structures & classes.
  • inline functions which do not create public symbols recognized by the linker.

Last edited by ocicat; 21st October 2008 at 01:53 AM.
Reply With Quote