Wird Question

Recommended Posts

Mortizar    122
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
Sneftel    1788
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
Antheus    2409
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
Description of an associative container, in case you don't know what one is. Think std::map.

Share on other sites
Mortizar    122
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
Deyja    920
std::map<int, function_pointer> event_handlers;event_handlers[some_event] = &handle_some_event;event_handlers[some_event](arguments);

Share on other sites
Zahlman    1682
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
Mortizar    122

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

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