"Greg Comeau" <co****@panix.comwrote in message
news:ec**********@panix2.panix.com...
In article <Dm***************@newsfe02.lga>,
Jim Langston <ta*******@rocketmail.comwrote:
>>Problem: I want to pass an Enum as an integer reference.
Solution:
void MyFunc ( int& MyInt )
{
std::cout << "Value is:" << MyInt << std::endl;
}
enum Enumerator
{
ZERO = 0,
ONE,
TWO,
THREE
};
int main()
{
Enumerator MyEnum = ONE;
if ( sizeof( MyEnum ) == sizeof( int ) )
MyFunc( *reinterpret_cast<int*>( &MyEnum ) );
}
Is this guaranteed to work if sizeof( Enumerator ) == sizeof( int ) ?
Not in C++.
>>By work I mean is this program guaranteed to output 1 in all cases?
Not sure what you're doing or why... why doesn't MyFunc just accept
an Enumerator, or just an int for that matter?
What I'm actually going to be using this for is to get the address of
MyEnum.
Which is why the function is not const int&, because if it is and someone
does
MyFunc( MyEnum);
the compiler will make a temporary int, load enum into it and pass that, and
taking the address of it gives me the wrong address.
Thinking about it though, the whole reason for this was so the user wouldn't
have to type
MyFunc( &MyEnum );
if I had
void MyFunc( const int* val );
but for enums I think it would be better for them to just pass the address
then this whole thing. I"ll just make a seperate function for that.
Thanks.