Difference Between Inline and Macro in C++

C++Server Side ProgrammingProgramming

In this post, we will understand the difference between inline and macro in C++.

Inline

  • It is a function in C++.

  • It is parsed by the compiler.

  • It can be defined inside or outside the class.

  • It evaluates the argument only once.

  • The compiler may not convert all functions to ‘inline’ function and expand them all.

  • The short functions that are defined inside the class are automatically made as inline functions.

  • An inline function inside a class can access the data members of the class.

  • Inline function can be terminated using curly brackets.

  • It is easy to debug.

  • This is because error checking is done during compilation.

  • It binds all statements in the body of the function.

Example

inline return_type funct_name ( parameters ) {
   . . .
}

Macro

  • It is expanded by the preprocessor.

  • It is defined at the beginning of the program.

  • It evaluates the argument every time it is used inside the code.

  • They always need to be/are expanded.

  • They need to be defined specifically.

  • They will never become members of class.

  • They can’t access data members of the class.

  • Definition of macro ends with the new line.

  • It is difficult to debug macros since error checking doesn’t happen during compile time.

  • It encounters binding problem if it contains more than one statement since it doesn’t have a termination symbol.

Example

#define macro_name char_sequence
raja
Published on 24-Mar-2021 13:22:23
Advertisements