Author Topic: c++ code: why would they do it this way and not...  (Read 3945 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
c++ code: why would they do it this way and not...
« on: December 22, 2010, 08:04:25 AM »
I saw this c++ code snippet in a program:
Code: [Select]
enum
{
ID_IsNotPickable = 0,
        IDFlag_IsPickable = 1 << 0,
        IDFlag_IsHighlightable = 1 << 1
};

Why wouldn't they just do:
Code: [Select]
enum
{
ID_IsNotPickable,
IDFlag_IsPickable,
IDFlag_IsHighlightable
};

Both would assign the same values:
ID_IsNotPickable = 0
IDFlag_IsPickable = 1
IDFlag_IsHighlightable = 2

Any ideas why they would use this method? The coder is the guy that wrote the irrlicht engine and he knows his stuff, so there must be a neat reason why he would do this.

Peter

  • Guest
Re: c++ code: why would they do it this way and not...
« Reply #1 on: December 22, 2010, 09:20:41 AM »

 it looks like, i'm the best!
 He isn't good and Irrlicht too!


Charles Pegge

  • Guest
Re: c++ code: why would they do it this way and not...
« Reply #2 on: December 22, 2010, 09:40:19 AM »
No it doesn't make sense to me either.

But I checked Oxygen's enumeration default value. It starts with 1, but to comply with the C standard I will change it to 0. [forthcoming Alpha024]

Charles

kryton9

  • Guest
Re: c++ code: why would they do it this way and not...
« Reply #3 on: December 22, 2010, 09:47:02 AM »
it looks like, i'm the best!
 He isn't good and Irrlicht too!

hehehehehe that is funny Peter :)

kryton9

  • Guest
Re: c++ code: why would they do it this way and not...
« Reply #4 on: December 22, 2010, 09:48:56 AM »
No it doesn't make sense to me either.
But I checked Oxygen's enumeration default value. It starts with 1, but to comply with the C standard I will change it to 0. [forthcoming Alpha024]
Charles

I would recommend making it as you have your arrays too Charles. So if they are 0 indexed then enums with 0 would be more consistent in my opinion, or if 1's then that would be fine with me.

Charles Pegge

  • Guest
Re: c++ code: why would they do it this way and not...
« Reply #5 on: December 22, 2010, 10:56:22 AM »
Kent,

My reason is to read C headers correctly when enumerations are defined. However I would expect most good headers to define the value of first members.

I feel we are committed to a default of indexbase 1 for arrays however. It makes dimensioning and iteration a bit cleaner and also conforms to character indexing in strings.

Charles

PS: you can set the indexbase to any integer value. It is also bound by scope so it can be safely altered within a function without affecting the rest of the program.

kryton9

  • Guest
Re: c++ code: why would they do it this way and not...
« Reply #6 on: December 23, 2010, 10:55:26 AM »
I go back and forth with 0 and 1 based arrays all the time. Each has their own good qualities. So no worries.