d****o 发帖数: 1055 | 1 有两个类
class File{
};
和
Class FileSystem{
Private:
vector files;
};
Class FileSystem{
Private:
vector files;
};
这里最好用指针还是对象本身?大牛来讲解一下。 |
h****e 发帖数: 928 | 2 可能pointer更好吧,这样FileSystem和File之间可以decouple。
在FileSystem.h里,只要声明class File;就可以了,不需要include
"File.h"。 |
d****o 发帖数: 1055 | 3 你这个解释没懂。
如果用pointer,是不是在FileSystem 的destructor 中就需要显式得 delete 每一个
file吧?要不会内存泄露。
【在 h****e 的大作中提到】 : 可能pointer更好吧,这样FileSystem和File之间可以decouple。 : 在FileSystem.h里,只要声明class File;就可以了,不需要include : "File.h"。
|
h****e 发帖数: 928 | 4 一般不建议用raw pointer,你可以用boost smart pointer:
http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/smart_ptr.h
要准备面试的话,最好再看看smart pointer是怎么实现的。
个
【在 d****o 的大作中提到】 : 你这个解释没懂。 : 如果用pointer,是不是在FileSystem 的destructor 中就需要显式得 delete 每一个 : file吧?要不会内存泄露。
|
d****o 发帖数: 1055 | 5 多谢,如果不用smart pointer呢?我觉得OOD问题不用那么深入吧。
我觉得用对象不太好,因为当你添加一个file进这个vector的时候,是创造了那个file
的副本。这样系统开销比较大。而且改变file的时候也不是改变原有的file,是改变了
副本。
【在 h****e 的大作中提到】 : 一般不建议用raw pointer,你可以用boost smart pointer: : http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/smart_ptr.h : 要准备面试的话,最好再看看smart pointer是怎么实现的。 : : 个
|
g****y 发帖数: 240 | 6 c++11 有rvalue reference。 应该可以消除你说第一个问题。 第二个问题,返回
reference就可以了。
file
【在 d****o 的大作中提到】 : 多谢,如果不用smart pointer呢?我觉得OOD问题不用那么深入吧。 : 我觉得用对象不太好,因为当你添加一个file进这个vector的时候,是创造了那个file : 的副本。这样系统开销比较大。而且改变file的时候也不是改变原有的file,是改变了 : 副本。
|