I understand what it is and how to use it, but I don't understand why you'd want to. It's more restrictive than an if statement and uses odd syntax. Why not just use an equivalent if statement?
it's preferable to else/if chains when there's lots of possibilities because it can often be compiled to a jump table and thus have greater performance.
Where an if/else chain will test every condition until it finds a match, a switch might be able to just jump to the right code without having to do any tests.
I've used it a lot in emulators where you have to check opcodes. In my NES emu I had 256 possibilities. A switch is great for something like that. Can you imagine a if/else chain with 256 checks?
Wow I didn't use it with strings yet, but I never knew it doesn't work with strings..
In C# I could switch almost anything, bool -> string..
So anyways, if i have something like 5-6 possibilities, Is it better to use a switch or a sequence of if/elses?
The switch as used in C++ is the same as that used in C - I can't see any reason (but I'm no expert) why it cannot be updated/expanded in C++ to include user defined types like classes.
As the case but is really an equality test - the class could overload the == operator appropriately.
For those things like comparing C strings then a global overload function could be provided by the programmer as well.
I'm not talking about a one/two possibilities of a string, or I would have never asked that, I'm talking about almost 5-6 possibilities where it looks to me cumbersome to sequence up if/elses, and I wasn't talking about C also :P, I was talking about C# anyways, so this can only accept integers.. and what about enums, don't they count as integers with variables?
[edit] Well, there is no switch statement in there for that example, but the point was turning a string into an integer index, which can be used with a switch...
To switch on a string, I use map<string, functor>. It trades off some flexibility (e.g. no missed "breaks", no access to locals) for better encapsulation.