Preprocessor help

This is a discussion on Preprocessor help within the C++ Programming forums, part of the General Programming Boards category; Hey! I'm learning C++ on my own with Practical C++ Programming by Steve Oualline. Pretty good book, but some of ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Preprocessor help

    Hey!

    I'm learning C++ on my own with Practical C++ Programming by Steve Oualline. Pretty good book, but some of the exercises are vague to say the least. I've made it pretty far but the chapter on the preprocessor has proven a real nightmare, so I figure I'd ask for help here.

    Exercise 10-1 for example, is "Create a set of macros to define a type called RETURN_STATUS and the following values: RETURN_SUCCESS, RETURN_WARNING, and RETURN_ERROR. Define a macro, CHECK_RETURN_FATAL,,, (sic) that takes a RETURN_STATUS as its argument and returns true if you have a fatal error."

    I guess that the person I really should ask is Steve Oualline, but I figure even if I could get in touch with him he has probably forgotten what he meant in the five years since the last edition was published. =)

    Anyway, ignoring the fact that I'm not sure I have understood the exercise, my solution would be the preprocessor equivalent of
    Code:
    if (input == RETURN_ERROR) return true;
    But how do I do that? I can't really do conditional logic from within a macro, right?

    Thanks in advance for any help you can offer!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Quote Originally Posted by royen
    I can't really do conditional logic from within a macro, right?
    You can, but in this case you just need a simple comparison, e.g.,
    Code:
    #define CHECK_RETURN_FATAL(status) ((status) == RETURN_ERROR)
    That said, I suggest that you read Stroustrup's answer to the FAQ So, what's wrong with using macros? in case Oualline has not stressed this.

    For example, instead of using a macro to define RETURN_STATUS, I might use a typedef, possibly of an enum. The enum would be used to define RETURN_SUCCESS, RETURN_WARNING, and RETURN_ERROR. Or I might use a class with static members (but ignore this if you have not learnt about classes yet). Instead of a macro for CHECK_RETURN_FATAL, I would use an inline function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    simply #define RETURN_ERROR, RETURN_WARNING and RETURN_SUCCESS as 1, <any integer> and 0 as this is common practice. Then typedef (better than #define) RETURN_STATUS as an integral type. Then add a macro called CHECK_RETURN_FATAL that compares its argument to RETURN_ERROR. I can't give you a solution here.

    EDIT: You were faster again

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2
    Great, thanks guys!

    Yeah, Oualline points out repeatedly that the preprocessor is a good way of introducing difficult-to-spot problems. All the more reason to learn how it actually works, I guess =)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21