【题目描述】

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. 配置nginx服务器 —— Nginx添加多个二级子域名

    1.安装nginx centos/linux下的安装Nginx 2.安装好后进入Nginx目录中 在conf目录下建立一个vhost(ps:名字自己设定)文件夹 其中的$NGINXHOME为你的ngi ...

  2. 分布式高性能消息处理中心HPMessageCenter

    # HPMessageCenter 高性能消息分发中心.用户只需写好restful接口,在portal里面配置消息的处理地址,消息消费者就会自动访问相关接口,完成消息任务. ### 部署说明 **创建 ...

  3. Shell脚本报错:-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory

    在学习shell中测试case参数命令代码如下 #!/bin/bash #switch测试 case $1 in     start)         echo 'start'     ;;      ...

  4. C语言_了解一下C语言中的四种存储类别

    C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. C语言中的四种存储类别:auto ...

  5. @EnableAsync @Asnc 以及4种拒绝策略

    根据不同的场景,可以选择不同的拒绝策略,如果任务非常重要,线程池队列满了,可以交由调用者线程同步处理. 如果是一些不太重要日志,可以直接丢弃掉. 如果一些可以丢弃,但是又需要知道被丢弃了,可以使用Th ...

  6. hi3531调用sil9024的驱动

    //测试mao  printf("\n\n");    unsigned int cmd=1;    unsigned long arg =4;  fd = open(" ...

  7. Javascript和JQuery中常用的随机数产生函数

    无论在普通Js框架或者JQuery中都可以使用的,函数如下,使用方法简介: 1)获取0-100的随机数--getRandom(100); 2)获取0-999的随机数--getRandom(999); ...

  8. java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class org.

    1.错误描述 严重: Exception starting filter struts2 java.lang.UnsupportedClassVersionError: Bad version num ...

  9. Java中的throw和throws的区别

    Java中的throw和throws的区别 1.throw关键字用于方法体内部,而throws关键字用于方法体部的方法声明部分: 2.throw用来抛出一个Throwable类型的异常,而throws ...

  10. sql一张表中两个字段指向同一个外键

    在项目开发中遇到这么一个例子,首先产品表 tb_product ----------------------------- id    name 1     手机 2    电脑 3     笔记本 ...