# Wird Question

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Quote:
 Original post by MortizarIs 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 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 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 on other sites
std::map<int, function_pointer> event_handlers;event_handlers[some_event] = &handle_some_event;event_handlers[some_event](arguments);

##### Share on other sites
Quote:
 Original post by MortizarFirst 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 on other sites

Thanks on this sort of thing I am not even sure what questions to ask

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631423
• Total Posts
3000007
×