I am little confused with the applicability of reinterpret_cast vs static_cast . From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word static . This is the cast the C++ compiler uses internally for implicit casts also. reinterpret_cast s are applicable in two scenarios, convert integer types to pointer types and vice ve
我对reinterpret_cast vs static_cast的适用性感到困惑。 从我读过的一般规则来看,当编译时可以解释类型时使用静态转换,因此使用static 。 这是C ++编译器内部用于隐式强制转换的强制转换。 reinterpret_cast s适用于两种情况,将整数类型转换为指针类型,反之亦然,或将一种指针类型转换为另一种类型。 我得到的一般想法是不可移植的,应该避免。 我有点困惑的地方是我需要的一种用法,我从C调用C ++,C代码需要保持C
如何将std::string转换为char*或const char* ? If you just want to pass a std::string to a function that needs const char* you can use std::string str; const char * c = str.c_str(); If you want to get a writable copy, like char * , you can do that with this: std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = ''; // don
如何将std::string转换为char*或const char* ? 如果你只是想将一个std::string传递给需要const char*的函数,你可以使用 std::string str; const char * c = str.c_str(); 如果你想得到一个可写的拷贝,比如char * ,你可以这样做: std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = ''; // don't forget the terminating 0 // don't fo
This question already has an answer here: Switch Statement Fallthrough…should it be allowed? [closed] 12 answers This is for the favour of "falling throught" cases: switch (x) { case 0: case 1: std::cout << "x is 0 or 1." << std::endl; break; } In this example, the case statement is executed if x is either 0 or 1. It is because the switch val
这个问题在这里已经有了答案: 切换声明Fallthrough ...应该允许吗? [已关闭] 12个回答 这是为了“跌宕起伏”的情况: switch (x) { case 0: case 1: std::cout << "x is 0 or 1." << std::endl; break; } 在这个例子中,如果x是0或1,case语句被执行。 这是因为switch val会被转换为程序集跳转到特定地址case (some value):是,那么CPU将继续正常执行代码,因此要获取下一个地址并
I'm having trouble understanding my C++ switch statement. I have to enter the accepted integer interval twice for the function to return to the switch. And then it falls straight through to case 2. Inherited Class: class Fugl : public DyrLuft { private: int alder; public: Fugl() : DyrLuft() { } void les() { do
我无法理解我的C ++ switch语句。 我必须输入两次可接受的整数间隔才能返回到开关。 然后它直接通过案例2。 继承的类: class Fugl : public DyrLuft { private: int alder; public: Fugl() : DyrLuft() { } void les() { do { cout << "nSkriv inn fuglens alder: "; cin >> alder; if(alder
This question already has an answer here: What does comma operator mean in a switch statement? 6 answers Just looking quickly at this code, we think the return value should be 1, I'd say that an experienced C++ developer would immediately notice that something is wrong and quickly conclude that some other programmer accidentally tried to use the comma operator: , but in the execution
这个问题在这里已经有了答案: 逗号运算符在switch语句中的含义是什么? 6个答案 只要快速查看此代码,我们认为返回值应为1, 我想说的是有经验的C ++开发人员会立即发现,什么是错的,并很快得出结论,其他一些程序员不小心用逗号: , 但在执行过程中它返回3。 不,代码不能编译 ,因为case表达式不是常量 事实上,它不会在任何半路现代编译器中编译。 例如,MSVC 2013说: stackoverflow.cpp(8) : error C2051:
The following code goes into an infinite loop on GCC: #include <iostream> using namespace std; int main(){ int i = 0x10000000; int c = 0; do{ c++; i += i; cout << i << endl; }while (i > 0); cout << c << endl; return 0; } So here's the deal: Signed integer overflow is technically undefined behavior. But GCC
以下代码在GCC上进入无限循环: #include <iostream> using namespace std; int main(){ int i = 0x10000000; int c = 0; do{ c++; i += i; cout << i << endl; }while (i > 0); cout << c << endl; return 0; } 所以这里是交易:有符号的整数溢出在技术上是未定义的行为。 但x86上的GCC使用x86整数指令实现整数运算 - 在溢出时换行。
The question itself begs an obvious answer. In any case, here's a snippet of my code... switch(cSet)... case 8:{ //Special Characters finalSet = special; char* charSet = new char[special.size() + 1]; charSet[special.size()] = 0; //Append null terminator memcpy(charSet, special.c_str(), special.size()); break; } case 9:{ //Alphnumeric
这个问题本身就是一个明显的答案。 无论如何,这里是我的代码片段... switch(cSet)... case 8:{ //Special Characters finalSet = special; char* charSet = new char[special.size() + 1]; charSet[special.size()] = 0; //Append null terminator memcpy(charSet, special.c_str(), special.size()); break; } case 9:{ //Alphnumeric and Special character
I'm a big fan of skipping the "default:" label in a enum-switch-case. So I get compiler warnings, when the enum has a new value which is not handled by the switch-case. Now a colleague stated, that when passing an integer to this switch-case which is not covered by the enumeration I will get a segmentation fault. At least the gcc we re using handles it correctly. Also the Visu
我非常喜欢在enum-switch-case中跳过“default:”标签。 所以我得到编译器的警告,当枚举有一个新的值,而不是交换机处理。 现在有个同事说,当把一个整数传递给这个没有被枚举所涵盖的开关情况时,我会得到一个分段错误。 至少我们正在使用的gcc正确处理它。 另外Visual Studio文档说:如果“没有任何常量匹配案例标签中的常量并且不存在默认标签>>>在switch语句之后,将控件传输到语句。” 这是否是标准,因此是
I'm programming a simple text-based RPG using a switch statement for a game loop. The program works fine until I attempt to add another case statement, at which point it gives me the following three errors: "jump to case label" (error occurs at the line of the newly added case), and two "crosses initialization of 'ClassName *objectName'"(errors occur when the new
我正在使用游戏循环的switch语句编写一个简单的基于文本的RPG。 该程序工作正常,直到我试图添加另一个case语句,在这一点上它给了我以下三个错误:“跳转到案例标签”(错误发生在新增案例的行),和两个“十字架初始化” ClassName * objectName'“(在情况2中创建新对象时发生错误)。 我会粘贴重要的代码,如果有人需要更多,请告诉我。 int main(void) { // add weapons to array Weapon *weaponList[12]; //
I am writing some Win32 program. and I meet a problem. I define an array of Point,just like this: POINT points[3]; and now I want to Initialize it, and I know this is illegal POINT points[3] = { (295,295),(200,200),(400,500) }; so I need the correct way. You can do it simply as POINT points[3] = { 295, 295, 200, 200, 400, 500 }; but a safer thing to do would be this POINT points[
我正在写一些Win32程序。 我遇到了一个问题。 我定义了一个Point数组,就像这样: POINT points[3]; 现在我想初始化它,而且我知道这是非法的 POINT points[3] = { (295,295),(200,200),(400,500) }; 所以我需要正确的方式。 你可以简单地这样做 POINT points[3] = { 295, 295, 200, 200, 400, 500 }; 但一个更安全的事情就是这样做 POINT points[3] = { { 295, 295 }, { 200, 200 }, { 400, 500 } }; 有趣的