in header files we use pre-processor making an inclusion guards so in view of avoiding multiple header files inclusion so we write:
// mytest.h
#ifndef MY_TEST_H
#define MY_TEST_H
// code here
int foo() { return 0; }
#endif
// main.cpp
#define MY_TEST_H // example unintentionally we define it or it is defined in other header file that is included here in main right before inclusion of mytest.h
#include <iostream>
#include "mytst.h"
int main()
{
std::coutcout << foo() << std::endl; // foo(): undeclared identifer
return 0;
}
- some drawbacks above: if in our source there's a macro with the same name as MY_TEST_H is defined before the inclusion of mytest.h will issue in failure of pre-processor in header mytest.h so it's content won't be included thus foo is undeclared identifier!
another way to achieve the purpose above (avoiding multiple header inclusion) and avoiding drawback of pre-processor inclusion guard is to use pragma in our header:
#pragma once
the question: is pragma better powerful than inclusion guards? if so why people are still using inclusion guards?