C++ #ifdef to do condition compilation

Description

C++ #ifdef to do condition compilation

#include <string>
#include <iostream>
using std::string;

namespace print1 {
  // Function prototype
  void print(const string& s);
}

namespace print2 {
  // Function prototype
  void print(const string& s);
}


void print1::print(const string& s) {
  std::cout << s << " (Namespace name is print1.) " << std::endl;
}

void print2::print(const string& s) {
  std::cout << s << " (Namespace name is print2.) " << std::endl;
}
void print_that(const string& s) {
  print2::print(s);/*  w ww  .  j a va2 s.  c om*/
}


void print_this(const string& s) {
  print1::print(s);
}

#define DO_THIS

#ifdef DO_THIS
#define PRINT(val) print_this(#val);
#else
#define PRINT(val) print_that(#val);
#endif

int main()
{
#ifdef DO_THIS
  print_this("This is a test string using printthis().");
#else
  print_that("This is a test string using printthat().");
#endif

  std::cout << "The print() function has been called a total of "      // ADDED
            << print_count << " times. " << std::endl;                 // ADDED
}



PreviousNext

Related