【题目描述】

Implement an assignment operator overloading method.

Make sure that:

The new data can be copied correctly

The old data can be deleted / free correctly.

We can assign like  A = B = C

实现赋值运算符重载函数,确保:

新的数据可准确地被复制

旧的数据可准确地删除/释放

可进行A = B = C赋值

【题目链接】

www.lintcode.com/en/problem/assignment-operator-overloading-c-only/

【题目解析】

这题就是考c++中的overload。题目要求先delete A,再copy B中的data。那么首先检查A的data是不是已经等于B了,如果是,直接return;如果不是,先delete A的data,然后new一个新的char*,把B的data copy到A中去。

【参考答案】

www.jiuzhang.com/solutions/assignment-operator-overloading-c-only/

Lintcode208 Assignment Operator Overloading (C++ Only) solution 题解的更多相关文章

  1. Default Assignment Operator and References

    We have discussed assignment operator overloading for dynamically allocated resources here . This is ...

  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. Operator overloading

    By defining other special methods, you can specify the behavior of operators on user-defined types. ...

  4. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

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

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

  6. C# to IL 5 Operator Overloading(操作符重载)

    Every operator overload that we use in C#, gets converted to a function call in IL. Theoverloaded &g ...

  7. copy constructor和copy assignment operator的区别

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

  8. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  9. [c++] Operator overloading

    c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...

随机推荐

  1. C语言_scanf()和getchar() 使用[粗俗易懂]

    原文地址:http://blog.csdn.net/hao5743/article/details/6939661/,以下是我重新整理的以下. 问题描述一:[分析scanf()和getchar()读取 ...

  2. opencv 离线文档下载地址在哪里?

    OpenCV 官方离线文档下载地址:http://docs.opencv.org/ 如何生成离线文档? http://docs.opencv.org/master/d4/db1/tutorial_do ...

  3. EmguCv“线段” 结构类型学习

    1. 文件所在 Namespace: Emgu.CV.Structure Assembly: Emgu.CV (in Emgu.CV.dll) Version: 3.0.0.2157 (3.0.0.2 ...

  4. Django 学习笔记

    day 1 : 一.web 框架本质: 1.http 建立在tcp 之上:一次互通后断开,无状态,短链接 请求头: b'GET / HTTP/1.1 Host: 127.0.0.1:8080 Conn ...

  5. python中__name__=='__main__'的作用

      学习python语法的过程中碰到了__name__=='__main__',这里做个笔记. 作用   这段代码的作用就是让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 测试 先 ...

  6. Linux shell的问题

    1.uptime命令可以查看当前系统的启动时间: w命令显示当前登录者top命令显示当前任务ps命令显示所有进程信息 uptime命令可以查看系统启动时间   2.使用shell时,默认的环境变量放在 ...

  7. shell脚本基础 数值运算 判断 及if语句

    数值运算 整数运算[三种,随便掌握一种即可]expr 数字 运算符 数字 [root@ceshiji ~]# expr 1 + 1(运算符号都是+ - * / 注:*需要\*.%是取余,余数只有0 1 ...

  8. JAVA的helloworld

    java环境设置------------- 在环境变量中设置以下三个变量:JAVA_HOME=C:\j2sdk1.4.1 //可以改为相应的目录CLASSPATH=%JAVA_HOME%\lib\to ...

  9. R语言︱构造新序列

    1.数值构造函数rep与seq #数值构造rep与seq rep(1:4,each=2)#依次重复1:4两遍 rep(1:4,2) #注意,重复1:4两遍 seq(from=3,to=5,by=0.2 ...

  10. 安装JBoss Tool 出错

    安装JBoss Tool 出错 具体报错如下: