Is there any alternative way of writing the code instaed of using switch statement in c++
We are building DLL's using C++ .As we are having 400开发者_JAVA技巧 cases in the size of the DLL is getting bigger in size so is there any alternative way of using instead of switch
Depends on the data type you're switching over. If it's a dense int range, use a lookup table storing function pointers. In other cases use a std::map
from switched-over values to function pointers.
I would say that the switching isn't the problem.
There's a whole lot of code duplication between your branches (I suppose you have something like 395 more) so you should be able to shrink your DLL by abstracting out the common parts; the only difference is what field you're retrieving and the calculation you perform with it. The vast majority of your code can be shared between branches if they're all as similar as those you've posted.
If your compiler isn't doing any pooling of strings, so you have several instances of your query templates, that isn't helping either.
Factor out all your conditions, DB queries and string concatenations into functions and see where that takes you.
You should do this anyway as it will make maintenance and further expansion much easier.
Relevant: my Dll size blown out by introducing large switch cases, how can I reduce my Dll size(MSVC C++)?
The size of the switch case and size of the Dll should have no direct relation. The only way size can be controlled is by reducing the common code and using STLs properly besides to the defalut optimization options ur compiler gives
精彩评论