草庐IT

case-statement

全部标签

c++ - if-cases 泄漏中定义的变量? (也就是为什么这甚至可以编译?)

似乎在if语句中声明的变量会泄漏到以下语句?我复制粘贴了一些代码,令我惊讶的是它在不应该编译的时候编译了!我正要提交代码,幸运的是我在那之前发现了错误。下面是一个显示问题的工作(?)程序。以下代码片段调用一个不存在的对象上的函数:#includeclassA{public:virtual~A(){}};classB:publicA{public:voidfooB(){std::cout(a)){b->fooB();}elseif(C*c=dynamic_cast(a)){c->fooC();b->fooB();}return0;}它编译并且输出是这样的:fooCfooB这肯定是错误的。

c++ - "Why switch statement cannot be applied on strings?"的答案是否仍然正确,即使使用 C++11/14?

我遇到了这个问题:Whyswitchstatementcannotbeappliedonstrings?并想知道答案是否:Thereasonwhyhastodowiththetypesystem.C/C++doesn'treallysupportstringsasatype.Itdoessupporttheideaofaconstantchararraybutitdoesn'treallyfullyunderstandthenotionofastring.仍然适用,即使在C++11/14中使用std:string。是否有多个elseif(...)的替代方案?

c++ - "virtual base class in the case of multilevel inheritance"有意义吗

考虑以下显示多级继承的示例代码:案例1:这里类derived1是通过虚拟继承从类base派生的,类derived2是从类派生的直接类derived1。classbase{};classderived1:virtualpublicbase{};classderived2:publicderived1{};Case2:与Case1相同,只是不涉及虚拟继承classbase{};classderived1:publicbase//novirtualinheritance{};classderived2:publicderived1{};假设我在这两种情况下都创建了derived2类的对象。C

c++ - 哪些提升类型用于 switch-case 表达式比较?

以下程序在使用不同的编译器编译时打印“unknown”。为什么会这样?#include"stdio.h"constcharOPTION=(char)(unsignedchar)253;intmain(intargc,char*argv[]){unsignedcharc=253;switch(c){caseOPTION:printf("option\n");break;default:printf("unknown\n");break;}return0;}在查看C++标准(N36902013-05-05)时,我看到了switch的子句:6.4.2Theswitchstatement2Th

c++ - 错误 C2196 : case value '?' already used

好的,在代码中使用VisualStudioUltimate2012构建时出现奇怪的错误(可能是ANSI、unicode等问题)...switch(input[index]){case'א'://AlefHebrewcharacterif(/*conditional*/){//Dostuff.}break;case'ב'://BethHebrewcharacterif(/*conditional*/){//Dostuff}break;default:{//Dosomeotherstuff.}break;}第二个case参数生成...ErrorC2196:casevalue'?'alrea

c++ - 使用 std::string 在 switch-case block 中返回字符串常量

注意:这不是关于使用字符串来选择switch-caseblock中的执行路径。C++中的一个常见模式是使用switch-caseblock将整数常量转换为字符串。这看起来像:charconst*to_string(codescode){switch(code){casecodes::foo:return"foo";casecodes::bar:return"bar";}}但是,我们是在C++中,所以使用std::string更合适:std::stringto_string(codescode){switch(code){casecodes::foo:return"foo";caseco

c++ - 为什么某些 IDE 不缩进 switch 中的 case 语句?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Whydon'tpeopleindentC++accessspecifiers/casestatements?我有一个语法问题...不是关于如何,而是关于为什么。许多IDE,例如Eclipse和QtCreator会自动缩进一个switch,如下所示:DayrandomDay=getRandomDay();/*returns'enumDay'*/switch(randomDay){default:caseMonday:/*...*/break;caseTuesday:/*...*/break;/*...*/}我

c++ - 在 switch 语句中使用 vector 时出现 "Jump to case label"错误。

这个问题在这里已经有了答案:Whycan'tvariablesbedeclaredinaswitchstatement?(23个回答)关闭9年前。这是代码,当我添加其他大小写或默认值时,会出现几个错误。我找不到任何基本错误,例如缺少分号等,并且当我只有一种情况时,代码可以正常工作。我搜索了switch教程,但我没有发现任何关于vector和switch语句混合的问题。intmain(){intr;while(cin>>r){switch(r){case3:inty=0;coutnums;intx;while(cin>>x){nums.push_back(x);y++;}sort(num

c++ - GCC 选项 : warning on non-void functions without a return statement

如果存在具有非空返回值但在其定义中不包含return语句的函数,是否有生成错误/警告的GCC/g++选项?例如:intadd(inta,intb){a+b;} 最佳答案 -Wreturn-type.它由-Wall(您应该始终与-Werror-Wextra一起运行)启用。 关于c++-GCC选项:warningonnon-voidfunctionswithoutareturnstatement,我们在StackOverflow上找到一个类似的问题: https:

c++ - g++ 表示 : warning: statement has no effect for shift bits operators

我正在实现alkhwarizmi算法。没错,但我的g++编译器不喜欢移位运算符:>>和当我编译它时,我得到这个输出:>g++-Wall-std=c++0x-o"Al-khwarizmialgorithm.o""Al-khwarizmialgorithm.cpp"(indirectory:/home/akronix/workspace/Algorithms)>Al-khwarizmialgorithm.cpp:Infunction‘intalkhwarizmi(int,int)’:Al-khwarizmialgorithm.cpp:31:9:warning:statementhasnoe