Sign in to follow this  

int/enum declaration bug?

This topic is 2043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was having a problem with a script where enum values were incorrect. Eventually, I found that in the script there was an int with the same name as an enum value, but there was no error indicating so. I guess since enums are pretty much the same as ints so it just overwrote the enum value, right? Is this a bug?

Share this post


Link to post
Share on other sites
Can you show in what circumstance you got the wrong value? I.e. give an example script that reproduces what you believe to be wrong.

An enum value can be implicitly cast to an integer. If the expected value was an integer or even a float or double, then the enum value and the integer value has equal rights, and the one that is chosen would depend on the scope.

If on the other hand the expected value was an enum, then only the enum should have been allowed, as the compiler should not do implicit casts of integers to enums.

Share this post


Link to post
Share on other sites
This is pretty much what I was doing:

C++:
engine->RegisterEnumValue( "Resource_Sound", "sndJump", 0);

Script:
class objPlayer
{
int sndJump; //Forgot to remove this when I decided to use enums instead;

objPlayer()
{
string text = sndJump;
printf( text); //I expected the enum value of 0 but instead it prints -842108451
}
}

It works as intended if I remove the 'int sndJump'. I just thought that this script should fail to build if this happens.

Share this post


Link to post
Share on other sites
This is not a bug.

The class member sndJump is in a different scope than the enum value registered by the application. When referring to sndJump in the class constructor it will find the class member first and use that.

To use the enum value in this case you need to define the global scope with "::" or more explicitly the scope of the enum itself with "Resource_Sound::".

The value -842108451 is because the sndJump member wasn't initialized before it was used.


I will however consider this situation. Perhaps I can make some improvements in the compiler to provide adequate warnings when a class member or local variable hides an enum or global variable. I may also be able to provide warning on use of a class member before it is initialized (in a constructor at least).

Share this post


Link to post
Share on other sites

This topic is 2043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this