Game Development Community

Torque Coding Guidelines: Why "#include"s before "#define"s?

by Steven Peterson · in Torque Game Engine · 03/03/2006 (9:37 am) · 8 replies

Torque Coding Guidelines

Here it clearly says #defines should go AFTER #includes. However this seems to be prohibitive if two classes need to include each other. By putting the #include inside the #define this is perfectly fine.

Unless for some Software-Engineering reason, this is the exact behavoir your trying to prevent with that guideline.. On the other hand, since the 'includes' are really a property of the object being defined, it's logical to flip it the other way around and place them inside the #define.

Can anyone offer some enlightenment on this?

#1
03/03/2006 (10:12 am)
You'll notice a little further down it specifically talks about preventing recurring inclusions using ifdefs:
Quote:
o prevent recurring inclusion of header files, all header files have an #ifndef directive immediately following the File Header Comment, and a corresponding #endif directive as the last line of the header file. The following example shows the proper format and nomenclature for a header file called BaseClass.h
#ifndef      _BASECLASS_H_
#define      _BASECLASS_H_
.
// rest of header file for baseclass.h
.
#endif      // _BASECLASS_H_
#2
03/03/2006 (10:22 am)
The example robert quoted is simply good/necessary programming practise in all settings, not just torque.

some compilers allow you to use this line to achieve the same effect:
#pragma once

but i highly recommend eschewing that in favour of good old standard ifndef..def..endif.
#3
03/03/2006 (10:27 am)
Raven -

also, yeah, it doesn't really make sense for #includes to "include each other".

possibly the situation you're thinking of is when you want to class definitions to each know about the other.

eg you want to achieve this:

classA.h
class classA
{
   classB* mClassB;
};

classB.h
class classB
{
   classA* mClassA;
};


.. in that case, what you want to do is, instead of #including everything everywhere, do this:
classA.h
class classB;   // just tells the compiler that there is some class called classB.

class classA
{
   classB* mClassB;
};

classB.h
class classA;   // just tells the compiler that there is some class called classA.

class classB
{
   classA* mClassA;
};


.. only works with pointers tho.
if you want to have actual instances in there,
i don't know how to do that.
#4
03/03/2006 (10:40 am)
@ Orion:
ok thats basically the case i'm refering to, they have to know about each other, and at least one is using pointers so I could do that.

@all:
I'm using the #ifndef stuff but, since it initially said #includes before #defines I was using this:

//-------------------------------------
// file: myClassA.h
// date: (today)
//-------------------------------------

// Guideline says #include's first
#include "myClassB.h"
#include "console/simbase.h"


//Guideline says #define's second
#ifndef _MYCLASSA_H_
#define _MYCLASSA_H_

//-------------------------------------
//  Begin class: myClassA
//-------------------------------------
class myClassA {
...

...
}  // end class: myclassA

#endif  // end #ifndef _MYCLASSA_H_


I thought those were the "defines" they were talking about, no?
#5
03/03/2006 (5:40 pm)
Was looking over this again. I'm guessing what you guys are trying to say is that they originally ment this:

// Class header here

#ifndef _MYCLASSA_H_
#define _MYCLASSA_H_

#include "myClassB.h"
#include "otherClass.h"

#define Other_Defines_Go_Here
#define Other_Defines_Go_Here

// Begin Class
class myClassA {
...

...
} // end class

#endif

correct?
#6
03/03/2006 (5:43 pm)
Hey raven - looks like i forgot to click "submit" on a message earlier today, sorry to leave ya hanging.
yeah, you've got it.
#7
03/03/2006 (5:48 pm)
Thanks for the help!

I'll cross-post the last code-sample onto the appropriet TDN discussion page in the AM.
#8
03/03/2006 (5:54 pm)
No sweat, it's fun stuff.