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

  • SEARCH
  • Home
  • 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 9148391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:15:35+00:00 2026-06-17T11:15:35+00:00

Hi I noticed if I include a header file in a .cpp then I

  • 0

Hi I noticed if I include a header file in a .cpp then I can create an object of that header file’s class. Like if I includeA.h in main.cpp then I can write A *a; in main.cpp. But this doesn’t work if I include a header file in another header file and then try to create the object of that included header file. Like,

file B.h:

#include "A.h"
class B
{
public:
    B(){};
    A *a;
};

I have to add forward declaration of the class A to make it work. Why?

  • 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-06-17T11:15:37+00:00Added an answer on June 17, 2026 at 11:15 am

    Here are the basics:

    • For any type A, if you declare a variable of type A&, A*, A**, A***,etc, then the compiler does not need to know the complete definition of A at the site of variable declaration. All it needs to know that A is a type; that is it. So a forward declaration is enough:

      class A; //forward declaration
      
      class B
      {
         A * pA;  //okay - compiler knows A is a type
         A & refA;/ okay - compiler knows A is a type
      };
      

      The complete definition is not required because the compiler can still compute sizeof(B) which in turn depends on sizeof(A*) and sizeof(A&) — these are known to the compiler, even though it doesn’t know sizeof(A). Note that sizeof(A*) is just a size of pointer on that platform (which is usually 4 bytes on 32bit system or 8 bytes on 64bit system).

    • For any type A, if you declare a variable of type A, A[N], A[M]N] etc, then the compiler needs to know the complete definition of type A at the site of variable declaration. A forward declaration would not be enough in this case.

      class A; //forward declaration
      class B
      {
         A a;  //error - the compiler only knows A is a type
               //it doesn't know its size!
      };
      

      But this is correct:

      #include "A.h" //which defines A
      
      class B
      {
         A a;  //okay
      };
      

      The complete definition is required so that the compiler could compute sizeof(A), which is not possible if the compiler doesn’t know definition of A.

      Note that definition of a class means “the complete specification of the class members, their types, and whether the class has virtual function(s) or not”. If the compiler knows these, it can compute the size of the class.

    Knowing these basics, you can decide whether to include headers to other headers or only forward declaration would be enough. If the forward declaration is enough, that is the option you should choose. Include a header only if it is required.

    However if you provide forward declaration of A in the header B.h, then you have to include the header file A.h in the implementation file of B which is B.cpp, because in the implementation file of B, you need to access the members of A for which the compiler requires the complete definition of A. Well again, include only if you need to access the members of A. 🙂


    Sorry I didn’t see the last paragraph of your answer. What is confusing me is why do I need the forward declaration also. Doesn’t including the header file A.h alone provides complete definition of class A?? –

    I don’t know what is there in the header file. Also, if in spite of including the header file, you also need to provide the forward declaration, then it implies that the header is implemented incorrectly. I suspect that there is a circular dependency:

    • Make sure that no two header files include each other. For example, if A.h includes B.h, then B.h must not include A.h, directly or indirectly.

    • Use forward declaration and pointer-declaration to break such circular dependency. The logic is pretty much straight-forward. If you cannot include A.h in B.h, which implies you cannot declare A a in B.h (because for this, you have to include the header A.h also). So even though you cannot declare A a, you can still declare A *pA, and for this a forward declaration of A is enough. That way you break the circular dependency.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that IE 9 does not support application cache. How can I use
I've been teaching myself cpp sporadically from 'accelerated C++' and recently I noticed that
I am using PHP and CodeIgniter. In every view file, I include the header.php
Please review the example code below, I have a class file that is loaded
I have noticed that a number of emails I get have a DKIM header.
I noticed that the <td align=center> code works differently on the MediaWiki.org page and
I noticed that there are two sets of Hadoop configuration parameters: one with mapred.*
I noticed that Clojure (1.4) seems to be happy to consider vectors equal to
I noticed that the first creation of an image from an uri takes pretty
I have started initially with VC++ 2008 Express. I've noticed that GCC becomes kind

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.