• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
robwenwzrdhat

C++ One header to rule them all

7 posts in this topic

My question involves header files in C++.

I currently have a few different classes and I separate them into a .h and a .cpp (such as regexClass.h and regexClass.cpp)

I have one header called theHeader.h and it includes all of my other headers:


[code]
#pragma once // or I use ifndefs as well here
#include <iostream>
#include <fstream>
#include <string>

#include "regexClass.h"
#include "classificationClass.h"

enum fileType {

ZIP = 0x4034b50,
GZIP = 0x88b1f,
SEVENZIP = 0xafbc7a37,
BZIP = 0x39685A42,
};

[/code]


when I build classifcationClass.cpp it requires access to the regexClass.h...... Instead of including regexClass.h, I just include theHeader.h.

[code]
#include "theHeader.h"
using namespace std;

classificationClass::classificationClass()
{
init();
}

classificationClass::init()
{
regexClass *theRegex = new regexClass(); //I can create a regexClass without having to "#include "regexClass.h" up at the top, since it is already included in "theHeader.h"

if(theRegex.match(ZIP, filePath))
cout<<"Victory"<<endl;
}

[/code]

This grants classificationClass.cpp access to regexClass, <iostream>, <string>, etc. without me having to re-include everything all over again. Since the compiler will only include the files once, this code would work and compile just fine. The other crucial element to this working, is the order of file inclusion. classificationClass uses a regexClass object, thus "regexClass.h" is included in "theHeader.h" before "classificationClass.h" is.

My coworker stresses to me that this is a bad programming practice, and wants to copy my "theHeader.h" includes into each of my separate class .h files. Is it bad practice to do this? Or am I just that awesome?

Thanks :)
0

Share this post


Link to post
Share on other sites
It is generally considered a bad idea. It will increase compile times a ton on large projects. Plus it doesn't clearly inform you of class dependencies, something you should always be aware of. Changing one header file will cause all the source files to rebuild, rather than just the source files that directly depend on that header.

Looking at your main header, it is not clear the order of the headers is significant. Just including regexClass.h in classificationClass.h makes the annoying 'order of inclusion' go away.
0

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1322589193' post='4888810']
It is generally considered a bad idea. It will increase compile times a ton on large projects. Plus it doesn't clearly inform you of class dependencies, something you should always be aware of. Changing one header file will cause all the source files to rebuild, rather than just the source files that directly depend on that header.[/quote]
What he said^.

However, it not unreasonable to place all your dependency inclusions (includes from system/3rd-party libraries you depend on) in a [b]pre-compiled[/b] header file - this can (in some cases) significantly speed up compilation.
0

Share this post


Link to post
Share on other sites
Most of us know about the #include "everything.h" trick, and most of us don't use it.

Does that tell you something?
0

Share this post


Link to post
Share on other sites
[quote name='swiftcoder' timestamp='1322589820' post='4888811']
[quote name='Cornstalks' timestamp='1322589193' post='4888810']
It is generally considered a bad idea. It will increase compile times a ton on large projects. Plus it doesn't clearly inform you of class dependencies, something you should always be aware of. Changing one header file will cause all the source files to rebuild, rather than just the source files that directly depend on that header.[/quote]
What he said^.

However, it not unreasonable to place all your dependency inclusions (includes from system/3rd-party libraries you depend on) in a [b]pre-compiled[/b] header file - this can (in some cases) significantly speed up compilation.
[/quote]

Yeah, this.

Frankly you are re-inventing stdafx, without any of the benefits. Look into pre-compiled header support. Generally you include large [b]unchanging! [/b]headers in a single include, but others always include the minimum amount necessary. Where the argument gets really interesting is forward declare vs including.
0

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1322589193' post='4888810']
It is generally considered a bad idea.[/quote]

Personally, I'd replace 'generally considered' with 'always'.


[size="2"][color="#1c2837"][quote][/color][/size]
[color=#1C2837][size=2]Since the compiler will only include the files once, this code would work and compile just fine.[/size][/color]
[color=#1C2837][size=2][/quote][/size][/color]
[color=#1C2837][size=2]No. It will include the files ONCE within a compilation unit, if, and only if, #pragma once is supported by the compiler (just because sentry guards prevent the source being included twice does not mean the compiler magically avoids opening & reading the contents of the header file).[/size][/color][color=#1C2837][size=2] Since a typical project may have a few dozen to a many tens of thousands of compilation units, you will be including far more than you need, and build times will rapidly spiral out of control (this is especially true for template heavy code). [/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color="#1c2837"][size="2"]Now, the oft used retort is "But it's only a small project, there's no need for me to worry about this!". Well, that's an invalid argument. There is always one certainty within any software project: Tomorrow the codebase will be larger than it is today. If you are sloppy with your includes today, you will have an enormous refactoring task looming in the distance. The only question you need to be asking is [i]when[/i] are you going to do the refactor? (hint: It's easier if you do it now!)[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[quote][color=#1C2837][size=2]My coworker stresses to me that this is a bad programming practice, and wants to copy my "theHeader.h" includes into each of my separate class .h files. Is it bad practice to do this? Or am I just that awesome?[/quote][/size][/color]
[size="2"][color="#1c2837"]
[/color][/size]
[size="2"][color="#1c2837"]It is an awful habit, and it's something you should unlearn as soon as possible. The good practice rules are this:[/color][/size]
[size="2"][color="#1c2837"]
[/color][/size]
[size="2"][color="#1c2837"]For header files: [/color][/size]
[size="2"][color="#1c2837"]* Minimise the number of includes to the absolute bare minimum (i.e. for base classes and member variable types only).[/color][/size]
[size="2"][color="#1c2837"]* Forward declare classes as much as possible to reduce the number of includes. [/color][/size]
[size="2"][color="#1c2837"]* To solve extreme build time problems, consider using the pimpl idiom. [/color][/size]
[size="2"][color="#1c2837"]
[/color][/size]
[size="2"][color="#1c2837"]For source files:[/color][/size]
[size="2"][color="#1c2837"]* Include the class header[/color][/size]
[size="2"][color="#1c2837"]* Only include additional headers as and when they are needed. [/color][/size]
[size="2"][color="#1c2837"]
[/color][/size]
0

Share this post


Link to post
Share on other sites
[quote name='RobTheBloke' timestamp='1322591060' post='4888816']
[quote name='Cornstalks' timestamp='1322589193' post='4888810']
It is generally considered a bad idea.[/quote]

Personally, I'd replace 'generally considered' with 'always'.
[/quote]
There was one guy who used to hang around here, I forget his handle, who actually advocated this, as long as the master header was a pre-compiled header. He said that it actually improved compile times for large projects even if the pch had to be rebuilt every time anything was modified. I don't agree with him, but there are people out there who think otherwise.

Going back to disadvantages, keep in mind that a master header's effects may extend beyond just compile times. Every compilation unit will generate it's own copy of any global variables with internal linkage or inside anonymous namespaces as well as inline function definitions and potentially language support structures such as virtual function tables. Some linkers can merge/discard duplicate copies of some of these, but some cannot and other variables cannot be discard because the language prevents it. One particular case is global variables with creation side-effects designed to work around the static initialization order problems like the Zerob counter.

Then there are the inevitable problems with identifier pollution, such as the ever so fun preprocessor definition issues that can arise if you force every single file in your project to include windows.h. Some of the preprocessor changes are well known and easy to avoid, like the regular all-caps identifiers. Some regularly bite even people who know about them like min and max. Others are just obscure out of the blue PITAs like rad0.
0

Share this post


Link to post
Share on other sites
Thank you very much for all of your responses! I knew this was the place to go to. I really appreciate it, and I will avoid using the "include everything" header from now on.
0

Share this post


Link to post
[size=2]Thank you very much for all of your responses! I knew this was the place to go to. I really appreciate it, and I will avoid using the "include everything" header from now on.[/size][quote]
There was one guy who used to hang around here, I forget his handle, who actually advocated this, as long as the master header was a pre-compiled header. He said that it actually improved compile times for large projects even if the pch had to be rebuilt every time anything was modified. I don't agree with him, but there are people out there who think otherwise.

Going back to disadvantages, keep in mind that a master header's effects may extend beyond just compile times. Every compilation unit will generate it's own copy of any global variables with internal linkage or inside anonymous namespaces as well as inline function definitions and potentially language support structures such as virtual function tables. Some linkers can merge/discard duplicate copies of some of these, but some cannot and other variables cannot be discard because the language prevents it. One particular case is global variables with creation side-effects designed to work around the static initialization order problems like the Zerob counter.

Then there are the inevitable problems with identifier pollution, such as the ever so fun preprocessor definition issues that can arise if you force every single file in your project to include windows.h. Some of the preprocessor changes are well known and easy to avoid, like the regular all-caps identifiers. Some regularly bite even people who know about them like min and max. Others are just obscure out of the blue PITAs like rad0.
[/quote]

I especially appreciate this response. I was looking for a concrete reason as to why this would be a bad habit, but have been unable to find one. Thank you. I will go and eat crow now!

[quote]

It is an awful habit, and it's something you should unlearn as soon as possible. The good practice rules are this:

For header files:
* Minimise the number of includes to the absolute bare minimum (i.e. for base classes and member variable types only).
* Forward declare classes as much as possible to reduce the number of includes.
* To solve extreme build time problems, consider using the pimpl idiom.
For source files:
* Include the class header
* Only include additional headers as and when they are needed.
[/quote]

[color=#1C2837][size=2]Thank you for providing me with appropriate practice rules as well. Everybody's experience is much appreciated. [/size][/color]
1

Share this post


Link to post
Share on other sites
[quote]

[size="2"][color="#1c2837"]It is an awful habit, and it's something you should unlearn as soon as possible. The good practice rules are this:[/color][/size]
[size="2"] [/size]
[size="2"][color="#1c2837"]For header files: [/color][/size]
[size="2"][color="#1c2837"]* Minimise the number of includes to the absolute bare minimum (i.e. for base classes and member variable types only).[/color][/size]
[size="2"][color="#1c2837"]* Forward declare classes as much as possible to reduce the number of includes. [/color][/size]
[size="2"][color="#1c2837"]* To solve extreme build time problems, consider using the pimpl idiom. [/color][/size][size="2"] [/size]
[size="2"][color="#1c2837"]For source files:[/color][/size]
[size="2"][color="#1c2837"]* Include the class header[/color][/size]
[size="2"][color="#1c2837"]* Only include additional headers as and when they are needed. [/color][/size][size="2"] [/size]
[/quote]

Thank you for providing me with appropriate practice rules as well. Everybody's experience is much appreciated.
0

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0