像Java一样管理对象:T&形式仅仅用在参数传递
类的对象为了关联/包含一个T类型的instance,若成员变量包括T*/ T&, 这种设计叫做“aggregation”(聚合);而若采用T 形式,则称为"composition"(组合)
//组合Composition
class Man {
Eye eye;
Nose nose;
} //聚合Aggregation
class Man {
Dog* dog;
House& house;
}
这个回答不错,百度知道:☛ 组合和聚合的区别?
怎样看待“引用类型作为类的成员变量”?
参考StackOverflow上此问题的回答:☛ Reference member variables as class members
尤其是其中 manlio的回答:
It's called dependency injection via constructor injection: (通过构造函数进行依赖注入)
class A
gets the dependency as an argument to its constructor and saves the reference to dependent class as a private variable.
For const-correctness I'd write:
using T = int;
class A
{
public:
A(const T &thing) : m_thing(thing) {}
// ...
private:
const T & m_thing;
};
but a problem with this class is that it accepts references to temporary objects:
T t;
A a1{t}; // this is ok, but...
A a2{T()}; // ... this is BAD. //临时的匿名对象 属于 rvalue
It's better to add (requires C++11 at least):
class A
{
public:
A(const T &thing) : m_thing(thing) {}
A(const T &&) = delete; // prevents rvalue binding
// ...
private:
const T &m_thing;
};
Anyway if you change the constructor:
class A
{
public:
A(const T *thing) : m_thing(*thing) { assert(thing); }
// ...
private:
const T &m_thing;
};
it's pretty much guaranteed that you won't have a pointer to a temporary.
Also, since the constructor takes a pointer, it's clearer to users of A
that they need to pay attention to the lifetime of the object they pass.
使用T&作为成员变量后:
①各个Contructor里必须对此T& t进行赋值。
②对象生成后就不能再对它进行赋值(=),因为引用不能二次赋值。
在此提问 ☛Should I prefer pointers or references in member data? 下, anon的回答:
As everyone seems to be handing out general rules, I'll offer two:
Never, ever use references as class members. I have never done so in my own code (except to prove to myself that I was right in this rule) and cannot imagine a case where I would do so. The semantics are too confusing, and it's really not what references were designed for. (引用& 最初就是为了 运算符重载时好看 而设计出来的)
Always, always, use references when passing parameters to functions, except for the basic types, or when the algorithm requires a copy.
These rules are simple, and have stood me in good stead. I leave making rules on using smart pointers (but please, not auto_ptr) as class members to others.
即:▶T& 形式仅仅用在 参数传递 ▶作为成员变量都用T* 形式 (绝不要用T&)。
像Java一样管理对象:T&形式仅仅用在参数传递的更多相关文章
- Java方法之定义形式及可变参数
目录 Java方法之定义形式及可变参数 方法调用 使用static修饰的方法 没有static修饰的方法 方法的定义格式 无参无返 无参有返 有参无返 有参有返 形参个数可变的方法 采用数组形参来定义 ...
- Java反射、动态加载(将java类名、方法、方法参数当做参数传递,执行方法)
需求:将java类名.方法.方法参数当做参数传递,执行方法.可以用java的动态加载实现 反射的过程如下: 第一步:通过反射找到类并创建实例(classname为要实例化的类名,由pack ...
- JAVA ArraySet<E> SET形式的有序LIST
Set形式的数组,数组内容重复 package com.sicdt.library.core.utils; import java.util.ArrayList; import java.util.C ...
- java中把指数形式的数字转为正常形式显示
/** * 当浮点型数据位数超过10位之后,数据变成科学计数法显示.用此方法可以使其正常显示. * @param value * @return Sting */ public static Stri ...
- java函数式编程的形式
java中没有真正的函数变量: 一.所有的函数(拉姆达)表达式,都被解释为functional interface @FunctionalInterface interface GreetingSer ...
- 【日常笔记】java文件下载返回数据流形式
@RequestMapping("/downloadFile") @ResponseBody public void download(String uploadPathUrl, ...
- java以流的形式输出文件
原文:http://blog.csdn.net/liutt55/article/details/78126614 public void downProcessFile(HttpServletRequ ...
- Java中注释的形式
单行注释 单行注释 // #双斜杠 快捷键:Ctrl + / 多行注释 多行注释 /* */ #单斜杠星号 星号单斜杠 快捷键:Ctrl + shift + / 文档注释 多行注释 /** */ #单 ...
- java面试题:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
答:是值传递.Java编程语言只有值传递参数. 当一个对象实例作为一个参数被传递到方法中时,参数的值就是该对象的引用一个副本.指向同一个对象,对象的内容可以在被调用的方法中改变,但对象的引用(不是引用 ...
随机推荐
- Mysql数据库之调整最大连接数
.查看最大连接数 show variables like '%max_connections%'; 2.修改最大连接数 set GLOBAL max_connections = 200; 以下的文章主 ...
- 使用adb 命令(atrace)抓起systrace的方法。
adb shell atrace -c -b --async_start -z gfx 1. 执行查看adb shell atrace 功能 atrace --h atrace: invalid op ...
- JMeter【第五篇】关联:5种方法
前几天在Q群里看到群友发的最近10年性能测试工具使用率的统计,最近的2018年,jmeter+loadrunner占了93%的使用率,说明这两个是主流,其中,jmeter的使用率逐年提升,现在已经超过 ...
- phpcms添加https后台分页错误的解决方法
今天修改一位客户的phpcms网站,他要求添加https,这对ytkah来说是轻车熟路了,但是后台稍微有点问题,点击分页出现错误,将鼠标移到下一页显示的链接是https://www.abc.com/w ...
- VIJOS-P1282 佳佳的魔法照片
洛谷 P1583 魔法照片 洛谷传送门 JDOJ 1396: VIJOS-P1282 佳佳的魔法照片 JDOJ传送门 Description 一共有n个人(以1--n编号)向佳佳要照片,而佳佳只能把照 ...
- 使用Git Flow进行版本控制
重置 # force reset $ git flow init -f
- 关于时间排序在ios中失效的处理方法
上个月公司做项目的时候在列表排序的时候产品加了一个需求,通过点击量,发布时间,评论量进行筛选的一个需求. 一开始在电脑上测试基本没问题,然后我也就放下了这个按耐不住的小心脏,然后在完成所有模块后 sh ...
- 在Hadoop-3.1.2上安装HBase-2.2.1
目录 目录 1 1. 前言 3 2. 缩略语 3 3. 安装规划 3 3.1. 用户规划 3 3.2. 目录规划 4 4. 相关端口 4 5. 下载安装包 4 6. 修改配置文件 5 6.1. 修改策 ...
- App3种开发方式的优劣分析:原生、混合和H5
App混合开发(英文名:Hybrid App),是指在开发一款App产品的时候为了提高效率.节省成本即利用了原生的开发技术还应用了HTML5开发技术,是原生和HTML5技术的混合应用.目前App的开发 ...
- db服务器参数优化
1.swap分区 swap作用是在系统内存不够的情况下,当做临时的内存使用. swap是在硬盘上,性能肯定没有再内存好,当系统内存使用超过40%的时候, swap会可能被使用,而系统一旦使用swap会 ...