Backup Macro

Backup Macro

Create a backup macro CREATE_BACKUP(type, var) that takes the type and name of a variable and creates a copy of the variable with the same name + _backup. Show its use by creating a backup with it and reffering to the backup.

Hint

use ## to concatonate 2 tokens.

Answer

#include <iostream>

#define CREATE_BACKUP(type, var) type var##_backup = var

int main() {
    int original = 42;

    // Create a backup of the variable 'original'
    CREATE_BACKUP(int, original);

    // Modify the original variable
    original = 100;

    // Display both the original and the backup variable
    std::cout << "Original value: " << original << std::endl;
    std::cout << "Backup value: " << original_backup << std::endl;

    return 0;
}
Questions