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 8587087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:28:51+00:00 2026-06-11T22:28:51+00:00

I have a class called A, which has its .cpp and .hpp files. It

  • 0

I have a class called A, which has its .cpp and .hpp files. It depends on classes B and C. Now, I’m wondering, should I #include B.hpp and #include C.hpp in A.cpp or A.hpp? I think it would be beneficial for me to #include in the A.hpp file, because I’m creating a vector of B*‘s, and I’m not sure that the compiler will know the correct size of B if I only forward declare it (so I couldn’t pointer increment correctly)… By “forward declaration”, I mean writing class B; and class C; in the beggining of A.hpp.

  • 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-11T22:28:52+00:00Added an answer on June 11, 2026 at 10:28 pm

    As already mentioned, you can often work-around having to #include stuff by using forward declarations, and indeed the best practice is to minimize the amount of other headers included.

    But: To answer your questions headline: yes you may, and the common mechanism to avoid “circular inclusion” is the following

    A.hpp:

    #ifndef A_HPP_INCLUDED
    #define A_HPP_INCLUDED
    
    ... // code
    
    #endif
    

    Now if say B.hpp includes A.hpp, and subsequently, C.hpp looks like:

    #ifndef C_HPP_INCLUDED
    #define C_HPP_INCLUDED
    
    #include "A.hpp"
    #include "B.hpp"
    
    ...
    #endif
    

    Then you won’t get an error stating that e.g. ‘class A’ has already been defined.

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

Sidebar

Related Questions

I have a class called Flamethrower which naturally has its own ammunition that is
I have a base class called Component which has a number of classes derived
I have a class called Trial which has_many results. Now What I want to
I have a class called Property which has nothing but get -methods. All the
Let's say I have an class called Star which has an attribute color .
I have a controller which has a method called history class UsersController < ApplicationController
I have an object class called BOM. It has a method, getChildItem() which returns
I have a base class with a property called Name, which has an XmlText
I have a class called Communicator which has an interface to an ElectronicDevice ,
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.

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.