![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
default option at the beginning of switch
How is this program supposed to behave? Is this even valid?
The default option is placed at the front of the switch statement, modifying the original value: #include <iostream> using namespace std; enum ENumber { eNum1 = 0, eNum2, eNum3 }; void TestSwitch(ENumber eNum) { switch(eNum) { default: eNum = static_cast<ENumber>(eNum3 - eNum); case eNum1: cout << eNum << " mapped to: " << eNum1 << endl; break; case eNum2: cout << eNum << " mapped to: " << eNum2 << endl; break; case eNum3: cout << eNum << " mapped to: " << eNum3 << endl; break; } } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: default option at the beginning of switch
sat_andi@yahoo.co.uk wrote:
> How is this program supposed to behave? Is this even valid? I don't know how it is supposed to behave, but it is syntactically correct. The order of the labels are not important. > The default option is placed at the front of the switch statement, > modifying the original value: It can modify the value if it wants to, as the switch (case selection) has already executed at that point. It will definitely not re-select a case. However, as there is no break for the default case, it will fall through to case eNum1. Is that really intentional?? > > #include <iostream> > using namespace std; > > enum ENumber > { > eNum1 = 0, > eNum2, > eNum3 > }; > > void TestSwitch(ENumber eNum) > { > switch(eNum) > { > default: > eNum = static_cast<ENumber>(eNum3 - eNum); > case eNum1: > cout << eNum << " mapped to: " << eNum1 << endl; > break; > case eNum2: > cout << eNum << " mapped to: " << eNum2 << endl; > break; > case eNum3: > cout << eNum << " mapped to: " << eNum3 << endl; > break; > } > } Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: default option at the beginning of switch
On Aug 20, 9:28 am, sat_a...@yahoo.co.uk wrote:
> How is this program supposed to behave? Is this even valid? Oh, it's valid, but Bo Persson pointed out its fatal flaw. If eNum is not one of the answers, you fix it, but go to the first case listed. The GCC assembly code shows that a switch statement is implemented as a bunch of if and goto statements. The code has already passed the gotos by the time they ever get to default and will not go back to them. Here's my suggestion, if you REALLY do want what you think you do. void TestSwitch(ENumber eNum) { switch( eNum ) { case valid_ans: break; default: TestSwitch( /* make eNum valid * / ); } } But, then again, my suggestion could cause an infinite loop if your solution for making eNum valid fails. Might want to make a testing variable that auto-returns if there were too many iterations. >void TestSwitch(ENumber eNum) >{ > switch(eNum) > { > default: > eNum = static_cast<ENumber>(eNum3 - eNum); .... > } >} The big logical flaw here is what makes eNum invalid. If it's less than zero, then 2 (eNum3) - neg will be greater than eNum3, thus MORE wrong! If it's greater than 2, then 2 - x will be negative! No matter what, this won't make eNum valid; rethink it. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: default option at the beginning of switch
On 21 Aug, 02:40, "Hak...@gmail.com" <Hak...@gmail.com> wrote:
> On Aug 20, 9:28 am, sat_a...@yahoo.co.uk wrote: > > > How is this program supposed to behave? Is this even valid? > > Oh, it's valid, but Bo Persson pointed out its fatal flaw. If eNum is > not one of the answers, you fix it, but go to the first case listed. > The GCC assembly code shows that a switch statement is implemented as > a bunch of if and goto statements. The code has already passed the > gotos by the time they ever get to default and will not go back to > them. > > Here's my suggestion, if you REALLY do want what you think you do. > > void TestSwitch(ENumber eNum) > { > switch( eNum ) > { > case valid_ans: break; > default: TestSwitch( /* make eNum valid * / ); > } > > } > > But, then again, my suggestion could cause an infinite loop if your > solution for making eNum valid fails. Might want to make a testing > variable that auto-returns if there were too many iterations. > > > > >void TestSwitch(ENumber eNum) > >{ > > switch(eNum) > > { > > default: > > eNum = static_cast<ENumber>(eNum3 - eNum); > ... > > } > >} > > The big logical flaw here is what makes eNum invalid. If it's less > than zero, then 2 (eNum3) - neg will be greater than eNum3, thus MORE > wrong! If it's greater than 2, then 2 - x will be negative! No matter > what, this won't make eNum valid; rethink it. { Edits: quoted clc++m banner removed. Since the banner is automatically appended to every article there is no need to quote it. -mod } My guess is that it is a poor attempt to convert negative values into Enums greater than the max one (Enum3). As stated above, this fails if eNum > eNum3 initially but even if that isn't a problem it would be more clearly written as eNum = static_cast<ENumber>( eNum < 0 ? (eNum3-eNum) : eNum ); // or expand to 'if' if you prefer -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|