Hello..
Is there any way to make a string switch in c++ to make my code look better instead of using if and else if for each case? Like in .net?
For instance:
Code:string str = "test"; switch(str) { case "test": break; case "test": break; }
Hello..
Is there any way to make a string switch in c++ to make my code look better instead of using if and else if for each case? Like in .net?
For instance:
Code:string str = "test"; switch(str) { case "test": break; case "test": break; }
No, since the switch may only be used with integral types, or a type convertible to an integral type. So even if it does compile, you would probably end up with a very wrong answer.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If you have a lot of strings, you can write a functions that work for each possible string, then put them in a map with the string as the key and the function pointer as the value. This is the command pattern. Then, when you have your string you just execute myFunctionMap[str].