Internal and external locks

I heard that you prefer to write internal rather than external external guards.
I searched on the Internet but could not find an answer to it.

This is a fragment of the book "C ++ Coding Standards" by Herb and Andrew, which shows the "external guard":

Avoid using outdated external security guards protected by older books:

#ifndef FOO_HJNCLUDED_ //NOT recommended
#include "foo.h"
#define FOO_HJNCLUDED_
#endif

Now this leads to the following question:

Q: What is an internal guard and an external guard? What is the difference between the two, and why do the internal ones include guards? I would like this answer to also give an example.

Edit: I ended up answering my question.

+4
source share
2 answers

- , , , .

foo.h " " (, " " , ).

// foo.h
#ifndef _FOO_H__
#define _FOO_H__

// ...

#endif  // _FOO_H__

bar.h foo.h foo.h. " ".

// bar.h
#ifndef _BAR_H__
#define _BAR_H__

#ifndef _FOO_H__
#include "foo.h"
#endif

// ...

#endif  // _BAR_H__

( ) , , , , , , , . , , .

+4

.

Guard:

" " :

header.h

#ifndef HEADER_H
#define HEADER_H

// Contents of include file

#endif

, , #include ed .
" ", .


External Include Guard:

, , , " ", .

header2.h

#ifndef HEADER_H
#include "header.h"
#endif

// Rest of header file goes here

: #ifndef HEADER_H - header.h. , , .
, .
, .


:

    • , # . .
+2

All Articles