【题目描述】

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. Angular4.0用命令行创建组件服务出错

    之前使用cnpm创建的angular4.0项目,由于cnpm下载的node_modules资源经常会有部分缺失,所以在用命令行创建模板.服务的时候会报错: Error: ELOOP: too many ...

  2. HopSpot虚拟机中的Mark word的作用

    1.其为对象头保存对象的hashcode 2.保存GC分代年龄,锁状态标志,线程持有的锁,偏向线程的ID偏向时间戳.

  3. keras初涉笔记【一】

    安装keras依赖的库 sudo pip install numpy sudo pip install scipy sudo pip installl pyyaml sudo pipi install ...

  4. Nginx下编译PHP+Mysql

    先说一下PHP在Apache和Nginx下所扮演的角色 apache一般是把php当做自己的一个模块来启动的. 而nginx则是把http请求变量(如get,user_agent等)转发给 php进程 ...

  5. handler机制面试

    handler面试必问 解析: Android提供了Handler和Looper来满足线程间的通信. Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(Message E ...

  6. 使用line_profiler查看api接口函数每行代码执行时间

    项目情景描述: 在restful架构风格的项目交付测试的过程中,某接口出现 请求超时导致的http 502 Bad Gateway,于是开始排查具体是接口函数中的哪行代码或函数 响应时间过长导致的50 ...

  7. Redis笔记3-redis事务

    Redis的事务机制允许同时执行多条指令,它是原子性操作,事务中的命令要么全部执行,要么全部不执行,另外,事务中的所有指令都会被序列化,而且其开始执行过程中,不回被即时过来的指令所打断,其需要经历三个 ...

  8. myeclipse8.5打包jar并引入第三方jar包

    用myeclipse自带的export工具,无法引入被引用的第三方的jar包,有两种方式: (1)直接export出jar包,解压jar包(第三方的jar包太多,太麻烦) 在创建好的JAR文件里找到M ...

  9. linux及hadoop修改权限

    linux下修改文件权限: 在shell环境里输入:ls -l 或者 ls -lh drwxr-xr-x 2 nsf users 1024 12-10 17:37 下载文件备份对应:文件属性 连接数 ...

  10. ubuntu11.04编译gm8180的ffmpeg

    1.1 About this documentThe ffmpeg is a open source package that provides video application for gener ...