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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:43:26+00:00 2026-06-02T07:43:26+00:00

Is it legal to forward declare in another header’s file? For example: #ifndef _MAIN_H_

  • 0

Is it legal to forward declare in another header’s file? For example:

#ifndef _MAIN_H_
#define _MAIN_H_
class ClassA;
class ClassB;
#include "classa.h"
#include "classb.h"
#endif

#ifndef _CLASSA_H_
#define _CLASSA_H_
#include "main.h"
class ClassA
{
public:
    ClassB b;
};
#endif

#ifndef _CLASSB_H_
#define _CLASSB_H_
#include "main.h"
class ClassB
{
public:
    ClassA a;
};
#endif

Both class A and class B depend on each other, and both have an object of the other type. What I did was forward declared both classes in another file. Is there a clean way to do this?

  • 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-02T07:43:27+00:00Added an answer on June 2, 2026 at 7:43 am

    In general it is legal to forward-declare classes across headers.

    However, in your example, you are instantiating both classes in each other, which is absolutely illegal!

    To show the reasoning behind this, think about the following:

    Let ClassA take 1 byte without b and ClassB also 1 byte without a.
    Now, include b: ClassA now takes 2 byte. Now include a: ClassB now takes 3 bytes. Now we have to update the size of ClassA to 4 bytes due to the size increase of ClassB. Following that logic ClassB is now 5 bytes, ClassA 6 bytes, ClassB 7 bytes…….

    To deal with this, you probably would like to change the type of (at least) one of a and b to a pointer or reference to the respective class. When doing this (in c/c++), make sure you understand your memory management!

    The solution to your original problem could then look as follows:

    ClassA.h

    #pragma once
    class ClassB;
    class ClassA
    {
    public:
        ClassB* b;
    }
    

    ClassB.h

    #pragma once
    #include "ClassA.h"
    class ClassB
    {
    public:
        ClassA a;
    }
    

    Note that ClassB.h requires ClassA.h since for demonstration purposes: ClassB contains a full object of type ClassA, so the definition of ClassA is required!

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

Sidebar

Related Questions

Is the following code the case of legal forward referencing? if yes why? public
Is it legal? class SomeClass { public: static void f(); }; using SomeClass::f; Edit:
Is this legal and/or good practice? #define SOFTWARE_VERSION_NUMBER 7.0v1.1 Want struct to always contain
Is it legal to define a default value for a parameter the way I
Code goes first: void foo(int x) { void bar(int); //is this forward-decl legal? bar(x);
Is this legal? If not, will the following code allow this? class Foo {
Why is this legal in PHP? <?php class Foo { public function test() {
It is legal to overwrite exported data in attached DLL/SO? Example (win32): t_Controller controller
Is it possible/legal to somehow encode CR/LF characters into a CSV file? (as part
Possible Duplicate: Is it legal to recurse into main() in C++? #include <iostream> using

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.