Incrementing enum types

This is a discussion on Incrementing enum types within the C++ Programming forums, part of the General Programming Boards category; Originally posted by Silvercord what about just making it an int. int DAY = MONDAY; DAY++; that should work for ...

  1. #16
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Silvercord
    what about just making it an int.

    int DAY = MONDAY;
    DAY++;

    that should work for ye
    Because then you would completely lose type-safety between different enumerated types.

  2. #17
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    so? enums are usually used as labels to help the programmer know what's going on. I usually only see them as labels for indices into arrays. enums are seen as ints anyway, you can have a function that takes an int and pass in an enumerated data type. Type safety is awesome!

    EDIT:
    You are really going to hate me poly, but here are the two options

    1) use an int
    2)
    What I usually do to make working with multiple different enum types quick and easy, as well as to avoid name conflicts, is I don't ever use the c++ enum type anymore, but instead use standardized integral types of defined length and format. This, firstly, makes your code much more easily to be portable. Next, I made a templated Enumeration class with 1 template parameter for the [usually] integral type to be used. The class completely consists of inline protected members and overloaded operators, and is simply used as a base from which to derive your "enums."

    Then, anytime you want a new type of enumeration, you create a new class for it, use private inheritance on the Enumeration type with the integral template parameter to optimize how much space you need for the type (IE an 8-bit unsigned int if the enumeration only needs a small range of possible values, or even an unsigned 64 bit integer if you are using a very large set of bitflags). Inside the class definition you define public static constants of the template type, which you use to act as your enum constants. Finally, you promote any member functions from the base to be public in the child, picking and choosing which functions to promote depending on whether or not it makes sense to, for instance, incremement an instance.

    You still get typesafety, although now on an object level. Also, since the enumerations are all privately derived from the Enum type, you can't convert to the base, which is good, because they should be thought of as completely different types.

    So now, whenever you need an enumeration of a certain type, you use the object type. You specify it's value by doing ObjectType::ConstantName. You only use the constant names as temporary objects for construction. You should never explicitly declare an object of the interal type.

    The only downside is that it's, of course, possible to construct the object with a different constant if the integral type is the same or if there is a built-in conversion between the types. This only happens during construction because everywhere else, you use the object's type, not the integral type. Also, if you consider other solutions, such as opengl's "super enum," they lack any form of typesafety on enumerations because they use exactly the same type for all of them (of course, since opengl is procedural there wasn't much of a choice)!

    Since the constants are public, static and nested in the class definitions, you never have to worry about name conflicts, but you can still access them externally. If you have ever worked with a lot of enumerations, you know what I'm talking about. For instance, it makes sence to have a None constant for many different enumerations which would normally result in conflicts. Since, with my implementation, the constants are encapsulated in their appropriate type definitions, name conflicts are never a problem, so you can have many types with enumerations of the same name, without ever having problems.

    Since the integral type is a template parameter, not only do you benefit from control of the size, etc. but it also makes it very simple to work with fileio. When using enum, you have no control of the size of the datatype (unless you use bitfields, though even then, you may have format problems and you have no way of creating a constant of larger size than your compiler's enum). With my implementation, you get low-level control with high-level benefits, and because all of the member functions are inlined, speed should not be an issue.
    I know using an int isn't 'correct' because you want your day to be its own data type. It just seems easier to use an int, it is what I would do. I pay a lot for ibuprofen as it is
    Last edited by Silvercord; 04-17-2003 at 07:25 AM.

  3. #18
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Silvercord
    so? enums are usually used as labels to help the programmer know what's going on. I usually only see them as labels for indices into arrays.
    They are also often used for bitflags, etc. If you wish to only look at the types as "labels," that's fine, but in C++, they are each completely new types, each with their own separate functionality, and they cannot be implicitly cast between by default.


    Originally posted by Silvercord
    enums are seen as ints anyway
    No... They are completely different types, there is just implicit conversion to ints (the typesafety comes from not being able to implicitly cast between other enum types. The reason enums can be implicitly converted to ints is most-likely just for compatability with C, which was much less typesafe than C++). It's the same reason that you can pass a char to a function that takes an int. That does not make a char "seen as" an int, does it

    Originally posted by Silvercord
    You are really going to hate me poly, but here are the two options

    1) use an int
    2) /* COOL STUFF GOES HERE */
    I'll take the latter
    Last edited by Polymorphic OOP; 04-17-2003 at 07:39 AM.

  4. #19
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    but it works as being seen as an int
    from the frustum culling tutorial from gts for example
    Code:
    enum PlaneData
    {
    	A = 0,				// The X value of the plane's normal
    	B = 1,				// The Y value of the plane's normal
    	C = 2,				// The Z value of the plane's normal
    	D = 3				// The distance the plane is from the origin
    };
    
    void NormalizePlane(float frustum[6][4], int side)
    {
    //BLAH
    }
    ...
    	NormalizePlane(m_Frustum, RIGHT);
    that's all I was getting at.

    EDIT: now you're probably ........ed off at me too.

  5. #20
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Silvercord
    but it works as being seen as an int
    from the frustum culling tutorial from gts for example
    that's all I was getting at.
    I know what you're getting at, but my point is that you can also call the function by doing

    NormalizePlane(m_Frustum, '\t') // char "being seen as an int"

    My point is that even though they can in some cases be implicitly converted between, they are still separate types with separate rules and different functionality.

    an enum isn't much more like an int than a char is, but the benefit of enums is that you have typesaftey between enums, they can have different operations, etc. Also note that the implicit conversion for enums is only one-way to ints.

    Originally posted by Silvercord
    EDIT: now you're probably ........ed off at me too.
    OMG DUDE, I HOPE YOU DIE!

    P.S. Kidding

    EDIT: HAHAHAHAHAHA Just kidding, I totally meant that.
    Last edited by Polymorphic OOP; 04-17-2003 at 08:20 AM.

  6. #21
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    OMG DUDE, I HOPE YOU DIE!

    P.S. Kidding

    EDIT: HAHAHAHAHAHA Just kidding, I totally meant that.
    oy vei

    How do you overload the ++ operator for an enumerated data type? I've never seen that done before.

    Hey Poly guess what? I'm starting to do BSP. I'm not writing a partitioner like you did (which seems really really cool), but I'm working on getting it loaded, and then setting up the binary tree to determine what sector you are in and then doing collision/frustum tests against those polygons, etc. I just started on it today, but I've got a clear idea of how it works and how the file is laid out.

  7. #22
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Silvercord
    oy vei

    How do you overload the ++ operator for an enumerated data type? I've never seen that done before.
    depends if you're refering to prefix ++ or postfix ++

    Code:
    // Prefix ++
    inline SomeType& operator ++( SomeType& Source )
    {
      return Source = SomeType( Source + 1 );
    }
    
    // Postfix ++
    inline const SomeType operator ++( SomeType& Source, int )
    {
      SomeType Temp = Source; return ++Source, Temp;
    }
    Originally posted by Silvercord
    Hey Poly guess what? I'm starting to do BSP. I'm not writing a partitioner like you did (which seems really really cool), but I'm working on getting it loaded, and then setting up the binary tree to determine what sector you are in and then doing collision/frustum tests against those polygons, etc. I just started on it today, but I've got a clear idea of how it works and how the file is laid out.
    Supar

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 11:57 AM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. enum types
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-31-2002, 08:14 AM

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