macro to print a string

How the PLUS macro work if i pass two strings to it, and it will parse them as the enum value? Thanks in advance. Sorry that i can't express myself too clearly.

#include "stdio.h"
#include <string>
#include <iostream>

using namespace std;

#define PRINT(Y) #Y
#define PLUS(X, Y) X+Y

int main( int argc, char *argv[] )
{
    typedef enum {
        FIRST,
        SECOND,
        THIRD
    };

    const char *a="THIRD", *b="SECOND";
    cout << PRINT(THIRD+SECOND is: ) << PLUS(a, b) << endl;
    return 0;
}

Answers


I think, from your followup comment, that I understand better what you're trying to do: Trying to make both a string form and non-string form of a symbol such as an enum value.

The way I usually do this is as follows:

#define Q(x) QQ(x)
#define QQ(x) #x

Then you use it with a non-string value, such as an enum:

enum { FRED = 1, BARNEY = 2 };

int main()
{
    std::cout << "The enum " << Q( FRED   ) << " has the value " << FRED   << std::endl;
    std::cout << "The enum " << Q( BARNEY ) << " has the value " << BARNEY << std::endl;
}

This prints:

The enum FRED has the value 1
The enum BARNEY has the value 2

If that's not what you're trying to accomplish, please clarify in your question and leave me a comment.


Need Your Help

Tomcat v7.0 Server setup for ports 8086 and 8009

java eclipse installation port tomcat7

I am new in java programming. I have write a small web program using struts. I ve already install apche tomcat 7 server. I configure this server to my project. Tomcat server error showing When I ru...

C: accessing members of struct in a function

c struct

I'm trying to use the struct member 'size' in my function print_shoe, but my for loop doesn't run. However, if I replace 'c->size' with an int in the for loop, it runs just fine

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.