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 ;
}

C++ explicit constructor/copy constructor note的更多相关文章

  1. c++ constructor, copy constructor, operator =

    // list::push_back #include <iostream> #include <list> class element{ private: int numbe ...

  2. default constructor,copy constructor,copy assignment

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  3. 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】

    先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream> ...

  4. C++-copy constructor、copy-assignment operator、destructor

    本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...

  5. C++ 类 复制构造函数 The Copy Constructor

    一.复制构造函数的定义 复制构造函数是一种特殊的构造函数,具有一般构造函数的所有特性.复制构造函数创建一个新的对象,作为另一个对象的拷贝.复制构造函数只含有一个形参,而且其形参为本类对象的引用.复制构 ...

  6. no copy constructor available or copy constructor is declared &#39;explicit&#39;

    今天新写了一个类.然后对这个类使用STL中的vector,碰到错误: no copy constructor available or copy constructor is declared 'ex ...

  7. Copy Constructor in Java

    Reference: TutorialPoints, GeekforGeeks The copy constructor is a constructor which creates an objec ...

  8. Copy Constructor的构造操作

    Copy Constructor的构造操作 有三种情况,会以一个object的内容作为另一个class object的初值: 1.  对一个object做显式的初始化操作 class X{…}; X ...

  9. 构造函数语义学——Copy Constructor 篇

    构造函数语义学--Copy Constructor 篇 本文主要介绍<深度探索 C++对象模型>之<构造函数语义学>中的 Copy Constructor 构造函数的调用时机 ...

随机推荐

  1. __construct 与 __destruct 区别

    其实这个问法是有问题的,__construct 与 __destruct 没什么可比性,两个方法一个在对象被创建的时候触发,另一个在对象被销毁的时候触发 具体可以翻阅PHP官方手册中的 http:// ...

  2. [shell] bash数组(for时排序)

    for处理时会自动把顺序按A-Z排序了 [root@XM-v106 ~]# bash b.sh A -> B -> C -> D -> E -> [root@XM-v10 ...

  3. Oracle中的SQL分页查询原理和方法详解

    Oracle中的SQL分页查询原理和方法详解 分析得不错! http://blog.csdn.net/anxpp/article/details/51534006

  4. 第十次作业psp

    psp 进度条 博文累积折线图 代码累积折线图 psp饼状图

  5. 【每日scrum】第一次冲刺day5

    请教以前做过类似软件的同学,受益匪浅,启发自己

  6. “吃神么,买神么”的第二个Sprint计划(计划过程内容)

     “吃神么,买神么”项目Sprint计划 ——6.1(第二天)立会内容与进度 团队组员各自任务: 陈键.吴舒婷:继续完善前台设局与布局 林欢雯.冯美欣:开展后台的界面的设计与布局 任务的进度: 陈键. ...

  7. iOS开发值得收藏的博客

    http://kobedai.me/ objc.io PS:经典,内容深而广objc中国NSHipster PS:很多小细节NSHipster 中文版唐巧的技术博客 PS:LZ是唐巧的脑残粉…OneV ...

  8. 前端基础(http协议相关篇)

    网络协议篇: 1.http请求过程 DNS解析——tcp三次握手——建立tcp连接后发起http请求——服务器响应http请求 ——浏览器得到资源——浏览器渲染 2.http报文 通用首部:可以出现在 ...

  9. RecyclerView 上拉加载下拉刷新

    RecyclerView 上拉加载下拉刷新 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/teach_s ...

  10. foo()与@foo()的区别

    1.@foo() 是错误控制输出,foo()是正常调用输出. 2.@符号在PHP 中可以忽略错误报告,对于表达式有提示错误的,但有不影响语句执行的,可以在表达式之前加@. 3.可以把@符号放在变量.函 ...