C++ explicit constructor/copy constructor note

C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用

 #include <iostream>
#include <vector>
#include <string>
#include <thread> class Demo
{
private:
int a;
public:
explicit Demo()
: a()
{
std::cout << "默认构造函数" << std::endl;
} ~Demo()
{
std::cout<<"析构函数"<<std::endl;
} explicit Demo(const Demo &other)
{
a = other.a;
std::cout << "拷贝构造函数" << std::endl;
} Demo &operator=(const Demo &other)
{
if (&other == this)
return *this;
a = other.a;
std::cout << "拷贝赋值构造函数" << std::endl;
return *this;
} }; void test(Demo& T)
{ } int main()
{
Demo c; //explicit constructor;
Demo D(c); //explicit copy constructor //test(D) //this is implicit,error; return ;
}
上一篇:Django(ORM查询2)


下一篇:04_web基础(五)之cookie与session