/*
* c++11关键字
* explicit
* 拒绝隐式转换
* 小鱼号的代码日志
*/
#include <QCoreApplication>
#include <iostream>
using namespace std;
struct A
{
A(int){}
A(int,int){}
operator int() const {return 0;}
};
struct B
{
explicit B(int){}
explicit B(int,int){}
explicit operator int() const {return 0;}
};
void handleA(A a)
{
}
void handleB(B a)
{
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
A a1 = 1;
//B b1 = 1; ///BAD
A a2(2);
B b2(2);
A a3 = {4,5};
//B b3 = {4,5}; ///BAD
int na1 = a1;
//int nb1 = b1; ///BAD
int na2 = static_cast<int>(a1);
//int nb2 = static_cast<int>(b1); ///BAD
A a4 = (A)1;
B b4 = (B)1; //OK
handleA(a1);
handleA(1);
handleB(b2);
//handleB(2);///BAD
return a.exec();
}
网友评论