Game Engine Architecture

(Ben Green) #1

120 3. Fundamentals of Software Engineering for Games


struct Foo // struct declaration
{
U32 mUnsignedValue;
F32 mFloatValue;
bool mBooleanValue;
};
Once a struct or class has been declared, it can be allocated (defi ned) in
any of the ways that an atomic data type can be allocated, for example,
z as an automatic variable, on the program stack;
void someFunction()
{
Foo localFoo;
// ...
}
z as a global, fi le-static or function-static;
Foo gFoo;
static Foo sFoo;

void someFunction()
{
static Foo sLocalFoo;
// ...
}
z dynamically allocated from the heap. In this case, the pointer or refer-
ence variable used to hold the address of the data can itself be allocated
as an automatic, global, static, or even dynamically.
Foo* gpFoo = NULL; // global pointer to a Foo

void someFunction()
{
// allocate a Foo instance from the heap
gpFoo = new Foo;

// ...

// allocate another Foo, assign to local
// pointer
Foo* pAnotherFoo = new Foo;

// ...

// allocate a POINTER to a Foo from the heap
Foo** ppFoo = new Foo*;
(*ppFoo) = pAnotherFoo;
}
Free download pdf