Game Engine Architecture

(Ben Green) #1
111

return (a > b)? a : b;
}

// definition of the min() function
int min(int a, int b)
{
return (a <= b)? a : b;
}

A pure declaration can be provided for a function so that it can be used in
other translation units (or later in the same translation unit). This is done by
writing a function signature followed by a semicolon, with an optional prefi x
of extern:


foo.h


extern int max(int a, int b); // a function declaration

int min(int a, int b); // also a declaration (the
// ‘extern’ is optional/
// assumed)
Variables and instances of classes and structs are defi ned by writing the
data type followed by the name of the variable or instance, and an optional
array specifi er in square brackets:


foo.cpp


// All of these are variable definitions:
U32 gGlobalInteger = 5;
F32 gGlobalFloatArray[16];
MyClass gGlobalInstance;

A global variable defi ned in one translation unit can optionally be declared for
use in other translation units by using the extern keyword:


foo.h


// These are all pure declarations:
extern U32 gGlobalInteger;
extern F32 gGlobalFloatArray[16];
extern MyClass gGlobalInstance;

Multiplicity of Declarations and Defi nitions


Not surprisingly, any particular data object or function in a C/C++ program
can have multiple identical declarations, but each can have only one defi ni-
tion. If two or more identical defi nitions exist in a single translation unit,
the compiler will notice that multiple entities have the same name and
fl ag an error. If two or more identical defi nitions exist in diff erent transla-


3.2. Data, Code, and Memory in C/C++

Free download pdf