Advertisement

The #include nightmare

Started by February 21, 2003 07:59 PM
5 comments, last by Drastick 21 years, 8 months ago
I am having an nightmare trying to get my header file to work as one. I thought that #ifndef NAME_H #define NAME_H would prevent redefinitions and such... what about #pragma once I often see this but don''t know what it means. In other words im lost anyone have a good format for dealing with this? Thanks
Either one works (for MSVC++). I''m not sure whether #pragma once works for other compilers.

What exactly is the problem?
Advertisement
I think that the #pragma once only works with MS Visual C++, but I may be wrong. As far as I know, this has the same effect as the code below - anyone?

On the other hand, I have used the #indef... coding before without a problem. Here is how I used it:

#ifndef __MY_H_FILE__
#define __MY_H_FILE__

//rest of include file code/definitions go here

#endif


Personally, I''m not too fussed which method is used, as long as one is consistent in the approach.

Cheers.
I guess my problems is..

1) When I make a lager program with lots of header files I always seem to have problems making sure everything is in a good layout.

2) Becase I am unhappy with the layout I went and tryed to clean all my headers up so it is less of a mess. Now it is worse..

so here is what I have

"dx8interface.h" this is a class that handles everyting with dx

I have a "system.h" that I want to be global to all my files and it includes "dx8interface.h" and also creates a class from dxinterface I called it dx

next I have my "gameinterface.h" and it need the class dx

then I have "blitterobject.h" which needs the class dx

I need blitterobject to be avalible for all the files that gameinterface need *eyes spin back into head* and now I am lost of where to put what where. If I don''t include "system.h" in blitterobject then I have a problem with the class dx that I wanted global...*sigh*
quote: Original post by Drastick
"dx8interface.h" this is a class that handles everyting with dx

I have a "system.h" that I want to be global to all my files and it includes "dx8interface.h" and also creates a class from dxinterface I called it dx


no no no no no. This file is your problem.

1) When you have a header file that defines a class, it should ONLY define a class. Nothing else.

2) Never EVER include header A in header B unless:
a) The class in B subclasses the class in header A
b) The class in B includes, or its functions return, an object of the class in header A (this does NOT include pointers to objects; see below)


3) If your class A has a pointer to a class B, do it like this in the header file:
class B;class A {    B *myBpointer;}; 


Follow these guidelines. Happiness will follow.

But... but that''s what HITLER would say!!
#pragma once only works in MSVC, however since that''s what I use, I find it to be a cleaner, shorter alternative to defines. Just my personal preference.

Like Drastick said, use a foreward declaration to have a pointer to a class. It''s just the

class B;

where the actual body of the class is defined in a different file, it just tells the compiler to accept the pointer for now, and the actual class will come later.

tj963
tj963
Advertisement
note:
#pragma only works on MSVC version 6 or above

Rate me up.

This topic is closed to new replies.

Advertisement