0

C++11 explicit关键字

 2 years ago
source link: https://forrestsu.github.io/posts/cpp/cpp-explicit-keywords/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++11 explicit关键字

2017年4月7日
| 字数 293
| CPP

C++中的explicit

class Test1
{
public:
    Test1(int n)
    {
        num=n;
    }//普通构造函数
private:
    int num;
};
class Test2
{
public:
    explicit Test2(int n)
    {
        num=n;
    }//explicit(显式)构造函数
private:
    int num;
};
int main()
{
    Test1 t1=12; //隐式调用其构造函数,成功
    Test2 t2=12; //编译错误,不能隐式调用其构造函数
    Test2 t2(12); //显式调用成功
    return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK