Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3406018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:34:36+00:00 2026-05-18T05:34:36+00:00

As a returning newbie to C++, I’m trying to sort the #include methodology. I’m

  • 0

As a returning newbie to C++, I’m trying to sort the #include methodology.

I’m following a certain set of guidelines I detail below the following example. So far this has worked out for me (the entire project keeps compiling 🙂 ), but I’m worried I may encounter problems in the future, therefore my questions are – is this a correct methodology? Is there a better one? What’s the underlying logic that explains it?

Consider the following example:

Father.h

#pragma once
class Father
{
    // Some implementation
};

ClassA.h

#pragma once
#include "Father.h"
#include "StructC.h"
class ClassB;
class ClassA : public Father
{
    StructC struct_c_obj;
    ClassB class_b_obj;
    // Some implementation
};

ClassA.cpp

#include "Father.h"
#include "ClassB.h"
#include "StructC.h"
// Some implementation

ClassB.h and ClassB.cpp
A class without includes

StructC.h

struct StructC {
    // Some implementation
};

I follow these guidelines:

  • All *.h are headed by a #pragma once declaration
  • If ClassA inherits from class Father, it must include it in both *.h and *.cpp file
  • If ClassA uses ClassB (and has a ClassB variable declared at the class’s scope), it has a class ClassB; decleration in ClassA.h and an #include "ClassB.h" in ClassA.cpp
  • If ClassA uses StructC (and has a StructC variable declared at the class’s scope), it has to include it in both ClassA.h and ClassA.cpp
  • If ClassA uses ClassD or StructE but only in the ClassA.cpp file, then it should include them only there

This is probably a clumsy set of guidelines with little understanding of the underlying logic, so I’m probably going to get some wrath… Bring it on, I am trying to learn here… 🙂

UPDATES:

  • As some have written below, I have an error in the example – you can use a forward declaration of ClassB in ClassA only if ClassA has a pointer or a reference to ClassB and not if it has a simple ClassB data-member.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:34:37+00:00Added an answer on May 18, 2026 at 5:34 am

    These are the guidelines I personally follow :

    • Prefer forward declarations instead of includes whenever possible. In your case, ClassA contains a ClassB so a #include "ClassB.h" is required. Had the ClassB type only appear in the file by pointer or reference, a forward reference would have been sufficient
    • Make header file “self sufficient” : compilation should never depend on the order of inclusions, and an include file should include / forward declare all it needs to be parsed
    • To ensure that the preceding guideline is respected, always include ClassA.h first in ClassA.cpp, and use an arbitrary ordering for the following includes (I’m using alphabetical sort)

    Regarding other aspects :

    • #pragma is non standard, prefer include guards
    • Keep in mind that you should never forward declare standard types : if std::string appears in your header file, you have to #include <string>
    • If you end up with a header file which includes a million other files, you might want to look into the pimpl idiom to reduce dependencies (this article also contains several other guidelines regarding header files).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.