Enum or Const Ints?

This is a discussion on Enum or Const Ints? within the C++ Programming forums, part of the General Programming Boards category; In my program I have something that would be like a status that is an int variable. Currently, the status ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    203

    Enum or Const Ints?

    In my program I have something that would be like a status that is an int variable. Currently, the status is being set by just using a number relevant to the actual status. Instead of just writing status = 1, status = 2 it's better to make the numbers as names, right? Would it be better for all the status names to be const int or put it all into an enum? In code it'd be like:
    Code:
    const int STATUS_ACTIVE = 0, /*etc*/;
    enum StatusEnum {STATUS_ACTIVE, /*etc*/};
    Also, the status is used across mutliple files. Const ints could be externed, but for enum would you extern a single one or create multiple variables of the enum?

  2. #2
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    or #define STATUS_ACTIVE 0

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Either the const int or the enum is fine (you can use an unnamed enum as well). I would just put the enum in a header file. Don't use the #define.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    it really depends on what you want. first as Daved said, don't use #define.

    in you case it looks like you have a bunch of related constants,
    i.e. STATUS_ACTIVE, STATUS_DEAD, STATUS_ON_A_COFFEE_BREAK.
    In this case, an enum is really best. In fact, you should use a StatusEnum everywhere you need this info in your code. The type system is there to help, after all.

    OTOH, if you have a single constant, something like MAX_NUMBER_OF_BEERS_BEFORE_DRUNK then it's better as a const int. (IMHO)

    Probably the most useful thing you can learn about these things is to scope them. Make everything as local as it can possibly be. That's why the macro is evil.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Thanks for the replies. They've given me a better understanding of when to use which.

  6. #6
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    use enum in combonation with namespaces. this way is better because enums are global to.

    this is an example of my enum code
    Code:
    namespace BEGIN{enum ENUM{	POINTS			= GL_POINTS,
    							LINE_LOOP		= GL_LINE_LOOP,
    							LINE_STRIP		= GL_LINE_STRIP,
    							TRIANGLES		= GL_TRIANGLES>,
    							TRIANGLE_STRIP	= GL_TRIANGLE_STRIP,
    							TRIANGLE_FAN	= GL_TRIANGLE_FAN,
    							QUADS			= GL_QUADS,
    							QUAD_STRIP		= GL_QUAD_STRIP,
    							POLYGON			= GL_POLYGON};}
    ...
    	static inline void Begin(const BEGIN::ENUM& mode) // inside someclass 
    	{
    		glBegin(mode);
    	}
    ...
    someclass.Begin(BEGIN::QUADS);
    this is the way i prefer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  3. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  4. vector<>
    By teval in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2003, 03:27 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM

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