Difficulty Level: Rookie

  Consider the following C++ program.

 1 #include<iostream>
2 #include<stdio.h>
3
4 using namespace std;
5
6 class Test
7 {
8 public:
9 Test()
10 {
11 }
12 Test(const Test &t)
13 {
14 cout<<"Copy constructor called "<<endl;
15 }
16 Test& operator = (const Test &t) //仅仅为测试
17 {
18 cout<<"Assignment operator called "<<endl;
19 return *this;
20 }
21 };
22
23 int main()
24 {
25 Test t1, t2;
26 t2 = t1;
27 Test t3 = t1;
28 getchar();
29 return 0;
30 }

  Output:
  Assignment operator called
  Copy constructor called

  Copy constructor is called when a new object is created from an existing object, as a copy of the existing object (see this G-Fact).

  And assignment operator is called when an already initialized object is assigned a new value from another existing object.

  t2 = t1;  // calls assignment operator, same as "t2.operator=(t1);"
  Test t3 = t1;  // calls copy constructor, same as "Test t3(t1);"

  References:http://en.wikipedia.org/wiki/Copy_constructor

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  21:23:27

Copy constructor vs assignment operator in C++的更多相关文章

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

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

  2. When should we write our own assignment operator in C++?

    The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need t ...

  3. Effective C++ 第0章 copy constructor和copy assignment operator

    拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...

  4. copy constructor和copy assignment operator的区别

    拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...

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

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

  6. Lintcode208 Assignment Operator Overloading (C++ Only) solution 题解

    [题目描述] Implement an assignment operator overloading method. Make sure that: The new data can be copi ...

  7. (C++)关于拷贝构造函数 Copy Constructor

    题目: In which of the following scenarios is a Copy Constructor called or invoked? A.    When no conve ...

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

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

  9. C++ explicit constructor/copy constructor note

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

随机推荐

  1. anaconda无法launch应用(无法l打开任何应用)的问题解决 (点击应用无反应)

    遇到了anaconda 无法launch 任何应用. 重装也不行. 先说我最终的解决方法(在官方文档中找到): 1. 启动 anaconda prompt , 输入 conda remove anac ...

  2. c++ IO库

    1:为了支持使用宽字符的语言,标准库定义了一组类型和对象来操作wchar_t类型的数据.宽字符版本的类型和函数的名字以w开头.宽字符版本和普通的char版本定义在同一个头文件中,例如头文件fstrea ...

  3. ansible简介安装配置

    ansible简介 ansible是一款,自动化运维管理工具.顾名思义是用于批量去管理及安装服务及批量管理主机. ansible与saltstack对比 ansible优点:配置简单,部署容易除主管理 ...

  4. thread pool

    thread pool import concurrent.futures import urllib.request URLS = ['http://www.foxnews.com/', 'http ...

  5. git修改用户和邮箱

    GIT查看当前用户以及邮箱 $ git config user.name $ git config user.email GIT修改用户以及邮箱 $ git config --global user. ...

  6. python 函数的定义及调用语法,map 方法,函数嵌套递归

    1.什么是函数    开发程序时候,需要代码执行多次,为了提高编写效率及代码重用性,所以把具有独立功能的代码块组织为一个小模块,给这个功能一个名称,这就是函数.    函数可以使用系统自带的函数也可以 ...

  7. 用户案例 | 腾讯小视频&转码平台云原生容器化之路

    作者 李汇波,腾讯业务运维高级工程师,目前就职于TEG 云架构平台部 技术运营与质量中心,现负责微信.QQ社交类业务的视频转码运维. 摘要 随着短视频兴起和快速发展,对于视频转码处理的需求也越来越多. ...

  8. .NET 百万级 大数据插入、更新 ,支持多种数据库

    功能介绍  (需要版本5.0.44) 大数据操作ORM性能瓶颈在实体转换上面,并且不能使用常规的Sql去实现 当列越多转换越慢,SqlSugar将转换性能做到极致,并且采用数据库最佳API 操作数据库 ...

  9. [atAGC050F]NAND Tree

    当$n$为偶数,暴力$o(n)$枚举第一次操作,以下只考虑$n$为奇数的情况 此时,$n-1$即操作次数为偶数,找到最小的$i$(其中$1\le i\le \frac{n-1}{2}$),满足第$2i ...

  10. Study Blazor .NET(四)数据绑定

    翻译自:Study Blazor .NET,转载请注明. 数据绑定 单向绑定 在blazor中单向绑定简单而直接,无需UI刷新或渲染.下面示例展示了单向数据绑定: //Counter.razor @p ...