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. Java学习(十九)

    先学了字体分类 大概是这种效果: 一般把这些大类放在font-family的最后使用,防止字体呈现的效果不够好. 今天把final关键字学完了 也要几点要注意,我觉得比较重要的就是public sta ...

  2. Python基础(作用域)

    def _private_1(name): return 'Hello, %s' % name def _private_2(name): return 'Hi, %s' % name def gre ...

  3. 大爽Python入门教程 3-5 习题

    大爽Python入门公开课教案 点击查看教程总目录 1 求平方和 使用循环,计算列表所有项的平方和,并输出这个和. 列表示例 lst = [8, 5, 7, 12, 19, 21, 10, 3, 2, ...

  4. eclipse下的python环境安装

    添加python开发环境到eclipse:   点击help--install New Software 点击add,弹出新窗口: Name:填PyDev Location:填 http://pyde ...

  5. Spring Aop面向切面编程&&自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

  6. [cf1491F]Magnets

    首先,只需要找到一个有磁性的位置,就可以通过$n-1$次判断其余磁铁是否有磁性,因此也就是要在$\lfloor\log_{2}n\rfloor+1$次中找到一个有磁性的位置 有一个$n-1$次的做法, ...

  7. [nowcoder5667H]Happy Triangle

    可以发现合法的答案有两种可能: 1.询问的$x$即为最大值(或之一),那么只需要找到x前两个数并判断即可 2.询问的$x$不是最大值,那么就要保证另外两边之差小于$x$,维护后缀中$的前驱k-k的前驱 ...

  8. c语言if语句是如何变成汇编代码的?

    1. 要编译的测试代码: int a; int b = 3; int main(void) { if (3) a = 4; else b = 5; } 2. 词法分析 词法分析将c源代码解析成一个个的 ...

  9. 痞子衡嵌入式:嵌入式Cortex-M系统中断延迟及其测量方法简介

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是Cortex-M系统中断延迟及其测量方法. 在嵌入式领域里,实时性是个经常被我们挂在嘴边的概念,这里的实时性主要强调得是当外界事件发生时 ...

  10. Go语言核心36讲(Go语言实战与应用十九)--学习笔记

    41 | io包中的接口和工具 (下) 上一篇文章中,我主要讲到了io.Reader的扩展接口和实现类型.当然,io代码包中的核心接口不止io.Reader一个. 我们基于它引出的一条主线,只是io包 ...