• function overloading problems
    7 replies, posted
I've got two overloaded functions to convert a number's endianness: __int16 SwitchEndianness( __int16 iValue ) { return ( (iValue>>8) | (iValue<<8) ); } __int32 SwitchEndianness( __int32 iValue ) { return ( ((iValue&0x000000FF)<<24) + ((iValue&0x0000FF00)<<8) + ((iValue&0x00FF0000)>>8) + ((iValue&0xFF000000)>>24) ); } The problem is when I call the function with a short as the argument is still uses the int version. Is there any way to stop this from happening, preferably without casting the argument?
Are you doing __int16 num = 0x10; num = SwitchEndianness(num); or __int16 num = SwitchEndianness(0x10);
I'm doing __int16 num = 0x10; std::cout << SwitchEndianness(num) << std::endl;
C++ can be dumb like this, gonna have to cast it. That or you can create a template.
c++ isn't dumb you're just doing it wrong :smug:
[QUOTE=efeX;18813949]c++ isn't dumb you're just doing it wrong :smug:[/QUOTE] Get out troll
He's not trolling. The code should work perfectly fine, and it does on my compiler. But then again the OP is using some obscure defines and hasn't told us on what architecture and what compiler he's trying it, so yeah.
[QUOTE=high;18813856]C++ can be dumb like this, gonna have to cast it. That or you can create a template.[/QUOTE] Maybe I'll try out the template option then. I'm doing this in Visual Studio 2008.
Sorry, you need to Log In to post a reply to this thread.