+ 1

Is a macro different from a header file or are they the same?

What I am confused about is if #define defines a header file or defines a macro. The article on SoloLearn about headers and source files says that it defines header files but a comment on the same article says otherwise.

2nd Mar 2018, 6:30 AM
Prasun
Prasun - avatar
1 ответ
+ 1
Thank you. One more question if I may, #ifndef tells the preprocessor to check if the header/macro has already been defined or not. If it has not been defined the compiler continues on to the next line which is #define and defines the header. If it has been defined the compiler skips to #endif. This prevents the same header from being included twice. Is all that correct? Also, is this how we use #pragma once: (1) Creating header file MyClass.h:- #pragma once MYCLASS_H #define MYCLASS_H class myClass { public: myClass(); ~myClass(); } (2) Including MyClass.h in the source file MyClass.cpp:- #include <iostream> #include "MyClass.h" using namespace std; myClass::myClass() { cout << "You just made an object." << endl; } myClass::~myClass() { cout << "An object was destroyed." << endl; } int main() { myClass obj; return 0; } // Output is "An object was created."
2nd Mar 2018, 9:57 AM
Prasun
Prasun - avatar