Sign in to follow this  
Mortizar

Wird Question

Recommended Posts

Is there a way to get a non-const variable to be taken as a case statement in a switch? What I mean is in the code below where is not a const variable. This question arises from a win32 wrapper that I am trying to make that wraps common controls as well. I am wondering if in any way it possible to store command identifiers in a structure and then retrieve them and have the retrieved value work in a switch statement.

   switch() {
 
       case variable:

           break;

  }


Share this post


Link to post
Share on other sites
Quote:
Original post by Mortizar
Is there a way to get a non-const variable to be taken as a case statement in a switch?

Assuming you're referring to C/C++? No. Use if-statements instead, or an associative container.

Share this post


Link to post
Share on other sites
switch statement gets interpreted at compile-time, so conditions cannot change during run-time. The jump table that gets generated tends to be optimized based on ranges, possibly even call probability.

For dynamic values, you'll need to use some form of lookup, or if/else statements.


void dynamic_switch( X x)
{
if ( x == variable1 ) {
} else if ( x == variable2 ) {
} else ...
}



This is equivalent to switch statement, but allows for variableX to change. It's also less efficient.

Share this post


Link to post
Share on other sites
First off thanks for your replies to this question. Secondly may I ask how an associative container would allow me to use a variable in a data structure to be used in a case statement?

What I want to do is create a command identifier for a toolbar button to be created at runtime and be stored in a structure that holds information about the button and then use that generated identifier in a case statement in the windows procedure to process that button. I know if's will do it but I would like the user of the class not have to go out of there way like that if at all possable. I messed with the stl map structure and it still gave the error that it’s not a constant expression. Is what I am trying to do completely imposable or do I just not know what I need to know to do this?

Share this post


Link to post
Share on other sites

std::map<int, function_pointer> event_handlers;

event_handlers[some_event] = &handle_some_event;

event_handlers[some_event](arguments);

Share this post


Link to post
Share on other sites
Quote:
Original post by Mortizar
First off thanks for your replies to this question. Secondly may I ask how an associative container would allow me to use a variable in a data structure to be used in a case statement?


You define a data type that represents an action to take, and then build an associative mapping from values to actions. The 'statement' is then replaced by looking up the condition in the mapping of values - which will then yield an action - and then executing the action.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this