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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:29:55+00:00 2026-05-27T13:29:55+00:00

C and C++ distinguishes between declarations an definitions. You can declare a symbol many

  • 0

C and C++ distinguishes between declarations an definitions.

You can declare a symbol many times, but you are allowed to define it only once. By learning this I have an idea to put declarations outside the guards, while definitions inside the guards:

// declarations

int foo(int x, int y);
int bar(int x, int y);

extern double something;

class X;

#ifndef _MY_HEADER_H_
#define _MY_HEADER_H_

#include "otherheader1.h"
#include "otherheader2.h"

// definitions of structures, classes.

class X
{
    // blah blah...
};

#endif

By this way I can include my headers whatever order I want. And probably circular dependencies won’t be a problem.

So why to protect the entire header with guard tokens if we can put the declarations outside?

My rationale was the following:

You probably often run into issues when two headers refer to each other somehow. You usually get an undeclared symbol error, and your first reflex is to include the necessary headers. But when two of your headers happen to include each other, you get cryptic errors.

a.h:

#ifndef A_H
#define A_H

#include "b.h"

class A {B *b;}       

#endif

b.h

#ifndef B_H
#define B_H

#include "a.h"

class B {A *a;}       

#endif

When in b.cpp you include the b.h you get an error in a.h that B is not declared but the header is included. (It’s a wtf moment.)

It’s because that header guards won’t nest:

#ifndef B_H
#define B_H

#ifndef A_H
#define A_H

// B_H already defined no include here.

class A {B *b;}       

#endif

class B {A *a;}       

#endif

If you put declarations outside the guard, you can prevent this:

class B; // in b.h

#ifndef B_H
#define B_H

class A; // in a.h

#ifndef A_H
#define A_H

class B; // again from b.h
// B_H already defined no include here, no redefinition.

class A {B *b;}       

#endif

class B {A *a;}       

#endif

No problem here.

UPDATE: put header inclusion into the guards (Sorry it was a mistake).

  • 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-27T13:29:56+00:00Added an answer on May 27, 2026 at 1:29 pm

    You’re missing half the story when you think only about “declarations”. C++ also has a notion of “class definitions”, which are a third, new type of animal — it’s both a definition (of the class) and a declaration (of the member functions).

    Since classes may not be defined more than once (just like for any definition), you must not include a header file with class definitions more than once.

    Now imagine you have some utility class Foo in foo.hpp, and you have two, independent modules a.hpp and b.hpp which both require Foo. Your main program must include a.hpp and b.hpp, but now you’re attempting to include foo.hpp, and hence the class definition of Foo, twice.

    Enter include guards.

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

Sidebar

Related Questions

I'm trying to find a CSS selector that can help me distinguish between the
Can I distinguish signal, between delivered directly to a process and delivered via debugger.
On http://code.google.com/apis/accounts/docs/OAuth2.html Google distinguishes between two kinds of tokens: refresh tokens and access tokens.
How can I get C# to distinguish between ambiguous class types without having to
Can I distinguish between compiled application from VS2008 Professional and Express?
I need a javascript regex that can distinguish between PHP tags in HTML tags
Suppose my input is ( a , b and c to distinguish between equal
Here are the brief definitions of encapsulation and abstraction. Abstraction: The process of abstraction
Classes and structs in C# share several characteristics: they can be instantiated (absent restrictions
I am reading the PCRE doc, and it refers to possessive quantifiers , but

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.