简体   繁体   中英

How does MYHEADER_HPP in a header guard relate to the actual file name "MyHeader.hpp"

I'm trying to teach myself C++ and one small detail that I don't understand keeps bugging me. I understand the need for header guards but I don't understand the exact syntax. For example. If I have a header file called MyHeader.hpp and I wanted to include it without guards I would write

#include "MyHeader.hpp"

However, all the tutorials I've looked at seem to indicate the way to do this with guards would be something like

#ifndef MYHEADER_HPP
#define MYHEADER_HPP

My question is how does MYHEADER_HPP in the #ifndef/#define equate to the actual file name "MyHeader.hpp" in the original include statement?

Thanks

You could use anything as your define and it would give the same result. The thing to look out for is if you would use the same name in two different files, you would get a problem if you try to include both in the same translation unit.

A very easy way to avoid that is to use header guards that matches the filenames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM