深拷贝与浅拷贝的认识

首先定义:

浅拷贝:直接复制对象里的字段(value)。若是指针则复制(value),这里的value是指address.非地址指向的内容

深拷贝:如果该字段不是指针,则复制字段,若存在指针,则申请一块新地址存放指向的内容。

拷贝构造函数没有处理静态数据成员

传值:在进入函数前,先通过拷贝函数(若没有拷贝构造函数,则调用默认构造函数,即浅拷贝)建立一个临时对象。函数结束时会调用析构函数销毁该对象。

传引用:把对象直接传过去

以下情况都会调用拷贝构造函数:
一个对象以值传递的方式传入函数体 
一个对象以值传递的方式从函数返回 
一个对象需要通过另外一个对象进行初始化。


一定要区别构造函数和赋值函数。就像声明和定义,初始化和赋值的区别。非常重要

这里感觉英文的书比较好理解,比如define or declaration,value or variables,initial or assignment.

比如:int a=10;//属于初始化,

int a=10;

int c=9;

c=a;//属于赋值;


声明时,初始化=操作,()操作都是调用拷贝构造函数,只有初始化完成后,=操作才是operator=,示例:
Type a;
Type b(a); // 拷贝构造函数
Type a;
Type b = a; // 拷贝构造函数
Type a;
Type b;
b = a;  // operator=(Type&)




string Literals:

I discuss string literals along with pointers because using astring literal in at program generates a ʺconstant pointer to character.ʺ When a string literal appears in an expression,the value used in the expressionis theaddresswhere its characters were stored, not the characters themselves.

Thus, you can assign a string literal to a variable of type ʺpointer to character,ʺ which makes the variable point to where the characters are stored. 

1 C++关于对象传值与传引用的总结  https://www.liuhe.website/index.php?/Articles/single/18 

2  

Java String 详解 - String Literal    http://www.iteye.com/topic/1122514



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值