enum and int

This is a discussion on enum and int within the C++ Programming forums, part of the General Programming Boards category; Hello I have a simple enumeration: Code: enum simple { one = 1, two, three }; Now I want to ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    enum and int

    Hello

    I have a simple enumeration:

    Code:
    enum simple {
    	one = 1,
    	two,
    	three
    };
    Now I want to compare enum with integer.

    Code:
    bool compare(simple e) {
    	int someint = 2;
    	return (someint == e) ? true : false;
    }
    Is this a proper way of doing this or it would be better to cast simpe enum to int (with static cast) before comparing it with integer:
    Code:
    int casted = std::static_cast<int>(e);
    where e is simple enum datatype.

    Many thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Why do you want to compare the enumerators with an integer in the first place?
    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
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Because I story http response/reply header this way:

    Code:
      enum status_type
      {
        ok = 200,
        created = 201,
        accepted = 202,
        no_content = 204,
        multiple_choices = 300,
        moved_permanently = 301,
        moved_temporarily = 302,
        not_modified = 304,
        bad_request = 400,
        unauthorized = 401,
        forbidden = 403,
        not_found = 404,
        internal_server_error = 500,
        not_implemented = 501,
        bad_gateway = 502,
        service_unavailable = 503
      } status;
    which I think its the best way of doing it. And then I have to compare it with the integers which are parsed out of the data.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why not use a std::map from int to string? The find function would seem to be exactly what you want.
    Last edited by tabstop; 10-07-2008 at 09:54 AM. Reason: Remove bad joke

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Shouldn't you be storing your responces into a status_type anyway? Its valid in C++ to store things to a status_type using istreams or however you are processing your HTTP requests.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    If you create your enum 'status_type' , you probably want to compare a variable to the values you've defined. So you should compare that variable with your enum, that's what it is ment for.

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by MarkZWEERS View Post
    If you create your enum 'status_type' , you probably want to compare a variable to the values you've defined. So you should compare that variable with your enum, that's what it is ment for.
    So by that definition its legal and okay to compare int with enum without casting it to enum?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's legal, and since enums are often used simply for defining a related group of integer constants (as you are doing), all the suggestions about storing a status_type or that are just overengineering.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CornedBee View Post
    It's legal, and since enums are often used simply for defining a related group of integer constants (as you are doing), all the suggestions about storing a status_type or that are just overengineering.
    You're probably right, but that's no fun. No, actually I was thinking you wanted to compare to all the enums or something, rather than one specific one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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