0
点赞
收藏
分享

微信扫一扫

explicit 关键字

JakietYu 2023-09-09 阅读 45

c++ 提供了关键字 explicit ,禁止通过构造函数进行的隐式转换。声明为 explicit 的构造函数不能在隐式转换中使用。

class MyString{
    public:
        explicit MyString(int n){
            cout << "MyString(int n)!" << endl;
        }
        MyString(const char* str){
            cout << "MyString(const char* str)" << endl;
        }
};
int main(){
    //给字符串赋值?还是初始化?
    //MyString str1 = 1;
    MyString str2(10);
    //寓意非常明确,给字符串赋值
    MyString str3 = "abcd";
    MyString str4("abcd");
    return EXIT_SUCCESS;
}

举报

相关推荐

0 条评论