2 types of C++ compiler guards

1.

#ifndef HEADER_BASE_HPP
#define HEADER_BASE_HPP

class base_class
{
  // some code
};

#endif // HEADER_BASE_HPP

 

The first line checks whether we have not yet defined 'HEADER_BASE_HPP'. If we have not, then we define it and include the rest of the file. If it is already defined, i.e. if this header file has already been included, then we will just skip until the end of the file and nothing more will get dumped into the '.cpp' file.

2.

#pragma once
// content of the header
class base_class
{
};

On Microsoft compilers guarding can be done using #pragma once, as the first pre-processor directive in a header.

原文地址:https://www.cnblogs.com/liunatural/p/1519026.html