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. XSS-DVWA

    1.反射型 LOW: 没有过滤,直接键入PAYLOAD 查看源码 这里没有任何过滤,使用htmlspecialchars()过滤 结果不弹窗 MEDIUM: LOW等级的方法不奏效了 观察输出可能是过 ...

  2. PHP处理表单数据的一个安全回顾(记录教训)

    曾经看过一个安全文章中写过这么一条 表单输入数据要做 htmlspecialchars_decode 表单输出数据要做htmlspecialchars 当时还不是很理解为什么,自己也没遇到问题,所以就 ...

  3. 关于inherit的笔记

    1. inherit是动态的 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  4. js中模拟a标签的点击事件

    var a = document.createElement('a'); a.target = "_blank"; a.href = "personal"; a ...

  5. cnblogs用户体验及建议

    一.是否提供了良好的体验给用户(同时提供价值)? 我觉得博客园还是给用户提供了良好的用户体验的,它可以从用户的角度考虑,用户在注册的时候,用户自己在设置用户名和密码的时候,如果与他人重复会有提示,而且 ...

  6. Task 4.3 求环形数组的最大子数组和

    任务要求:输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.    如果数组A[0]……A[j-1]首尾相邻,允许A[i-1], …… A[n- ...

  7. java对文件的操作

    1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**      * 以字节为单位读取文件,常用 ...

  8. SQLSERVER 的资源限制

    https://docs.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server?view= ...

  9. git 常用命令(含删除文件)

    git 常用命令(含删除文件) Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库: ...

  10. DAY1-Flask项目

    1.pipenv:与virtualenv类似的第三方的Python运行虚拟环境 给每个项目安装pipenv环境:pipenv install 启动:pipenv shell 使用pipenv安装Fla ...