in Education by
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). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    C and C++ distinguishes between declarations an definitions. You can declare a symbol many times, but you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    C and C++ distinguishes between declarations an definitions. You can declare a symbol many times, but you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    C and C++ distinguishes between declarations an definitions. You can declare a symbol many times, but you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I can not seem to be able to read from config file multitoken options like I can from command line ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    To my amazement I just discovered that the C99 stdint.h is missing from MS Visual Studio 2003 upwards. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Is there a way to utilise the GCC compiler whilst still being able to develop via the Visual Studio IDE ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    Is there a way to utilise the GCC compiler whilst still being able to develop via the Visual Studio IDE ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    Is there a way to utilise the GCC compiler whilst still being able to develop via the Visual Studio IDE ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    What is predicting y for a value of x that is within the interval of points that we saw in the original ... Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    How to read an excel sheet and put the cell value within different text fields through UiPath? I have a excel ... help me out please? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Which of the following is correct Content-type header that a server side script should send for SSE in HTML5? A - Content- ... Content-Type: text/data-stream D - None of the above....
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    When I do this: std::vector hello; Everything works great. However, when I make it a vector of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
...