为什么 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;
}

输出:

mystruct Bool 1
  1. 为什么 const char* 隐式转换为 bool 而不是 std::string,尽管构造函数需要 explicit 类型?
  2. 隐式转换优先级在此处如何应用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)