Book Image

Learning C++ by creating games with UE4

By : William Sherif
Book Image

Learning C++ by creating games with UE4

By: William Sherif

Overview of this book

Table of Contents (19 chapters)
Learning C++ by Creating Games with UE4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Variables and Memory
Index

Macros


C++ macros are from a class of C++ commands called preprocessor directives. A preprocessor directive is executed before compilation takes place.

Macros start with #define. For example, say we have the following macro:

#define PI 3.14159

At the lowest level, macros are simply copy and paste operations that occur just before compile time. In the preceding macro statement, the 3.14159 literal will be copied and pasted everywhere the symbol PI occurs in the program.

Take an example of the following code:

#include <iostream>
using namespace std;
#define PI 3.14159
int main()
{
  double r = 4;
  cout << "Circumference is " << 2*PI*r << endl;
}

What the C++ preprocessor will do is first go through the code and look for any usage of the PI symbol. It will find one such usage on this line:

cout << "Circumference is " << 2*PI*r << endl;

The preceding line will convert to the following just before compilation:

cout << "Circumference is " << 2*3.14159...