由买买提看人间百态

topics

全部话题 - 话题: smartptr
(共0页)
b***y
发帖数: 2799
1
☆─────────────────────────────────────☆
rebatezhq (test) 于 (Sat Aug 27 09:49:01 2005) 提到:
很多人在这说我的方法和auto_ptr/SmartPtr没有区别。
这是只看到表面现象。
auto_ptr/SmartPtr的设计目的是管理指针的,其名字说明了一切。
但是,动态内存分配大多数情况下却是用于数组的。虽然在C++数组
和指针通常可以通用,但这一点点设计目的的区别就产生了问题。
比如,auto_ptr所管理的对象可以这样用:ap->...,但不能ap[i]=...
auto_ptr也不能检测数组越界,这是个比内存泄漏更麻烦的问题。
我的方法设计目的是针对数组的。这同样可以从名字看出来。事实上
我根本没有重载指针的运算(->和*)。对我来说,单个的对象是不适
合用指针的,而应该用引用。因为引用运算(.)的书写复杂度底。
不要小看这一个字符的差异,如果你要对一个大结构赋初值就知道了。
当然你可以在TArray中加入支持指针的东西。我个人认为是没有用处
的。而我的方法的另一个好处是你可以加很多
H*M
发帖数: 1268
2
来自主题: JobHunting版 - 问个关于c++ auto_pter的问题
是的.它smart pointer那节讲到了.
就是想让T是base的时候,U是derived也可以.
like
void function(const smartPtr& p);
smartPtr p2(new Derived());
这个p2可以被传进去.
z***e
发帖数: 5393
3
来自主题: JobHunting版 - 请问有好的C++/STL的问题归纳么?
一般不会问太多stl问题,不过autoptr/smartptr需要了解.
B********8
发帖数: 9
4
来自主题: JobHunting版 - 请教一段volatile和多线程的代码
Are they from this page: http://drdobbs.com/cpp/184403766?
There are basically two points here:
1) LockPtr is a smartptr. It's ctor will call Mutex.aquire(); and its dtor
will call Mutex.release().
2) (This is harder) The smart usage of "volatile" and "const_cast<>" to make
user defined class variable/function thread-safe.
a) "volatile" object can NOT access normal members, such as "begin()", "
end()". It must be cast to "const" using const_cast<>
b) LockPtr will do this cast for you, when i... 阅读全帖
S*****n
发帖数: 227
5
来自主题: Programming版 - 一个interview问题,关于内存泄漏
using smartptr sometimes helps.
n******t
发帖数: 4406
6
我个人认为最好不要用smartptr.
如果你掉的库要用,你就用。
a********n
发帖数: 1287
7
来自主题: Programming版 - C++的smart pointer注定是个二流的东西
哈哈,想当年google面我让我实现一个smartptr的模板类。
顿时麻爪了。
只会用,不会写。
b***i
发帖数: 3043
8
来自主题: Programming版 - C++含有指针成员的类
我觉得应该是指针还有效的才能看到计数。
试验两句就懂了
在第一个{}里面去掉把ptr2加入的语句。可以看到,ptr2所指向的对象在reset那里被
销毁了
然后第二个{}那里,
std::cout << " but list1 has " << ptrList[0].use_count() << std::endl;
虽然ptr1不能访问那个A对象实例了,在vector哪里呢。所以不能执行析构函数。
那个不用smartptr的例子呢?
(共0页)