小编pmp*_*pmp的帖子

为什么 const char* 隐式转换为 bool 而不是 std::string?

#include <iostream>
#include <string>

struct mystruct{
     mystruct(std::string s){
        
        std::cout<<__FUNCTION__ <<" String "<<s;
    }
    
     explicit mystruct(bool s) {
        
        std::cout<<__FUNCTION__<<" Bool "<<s;
    }
};


int main()
{
    
    const char* c ="hello";
    
    mystruct obj(c);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

mystruct Bool 1
Run Code Online (Sandbox Code Playgroud)
  1. 为什么const char*隐式转换为bool而不是std::string,虽然构造函数需要explicit类型?
  2. 隐式转换优先级在这里如何应用?

c++ explicit constructor-overloading overload-resolution implicit-conversion

8
推荐指数
1
解决办法
164
查看次数

同时循环C中的开关案例

我最近遇到了下面显示的代码片段,我原以为它是一个语法错误,但令我惊讶的是,代码产生了有效的输出.

#include <stdio.h>

int main(void) {
    int x = 2;

    switch(x) {
    case 1: printf("1"); break;

        do {
            case 2: printf("2 "); break;
            case 3: printf("3 "); break;
        } while(++x < 4);

        case 4: printf("4"); break;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
output: 2 4
Run Code Online (Sandbox Code Playgroud)

编译器:GCC 6.3

我发现了一个类似的问题,但它并不能完全证明上述条件, 在C中混合'切换'和'同时'

谁能解释一下,

  1. 究竟发生了什么?
  2. 为什么不是语法错误?
  3. 为什么会跳过"3"的情况?

c while-loop switch-statement

4
推荐指数
1
解决办法
380
查看次数