To: do_do先生和其他高手先生 ->

forest_wawa 2001-12-28 10:03:27
do_do先生,

here I have some further questions as below:

U said,"Use smart pointer will save you lot of headache in large system if you have STL and can afford the little performance hit. "
1. I'vnt caught ur meaning, u mean STL toll much more overhead?

2. What is a smart pointer? >:)
u mean a pointer has "void" or "mutable" declaration?

3. U said, "But there are many cases you need to pass the ownership around.", but why transfer the owership around? I think it is not a good does, coz it maybe coz some unexpectable, right? >:O

p.s. Could u kindly tell me, what is a do_do? >:)
...全文
190 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
forest_wawa 2001-12-29
  • 打赏
  • 举报
回复
昨天公司服务器当掉,>:do_do先生,生两小孩可是一个大投资,得先买房子,再装修买家具,再再还得买老婆,等等等等...我爱看樱桃小丸子>:O
Faint,俺还没有学好赚钱本领,先缓一缓吧

谢谢pinel()先生,谢谢大家的帮助!>:(O~~~
>:(|)
lhj 2001-12-28
  • 打赏
  • 举报
回复
智能指针就智能指针,何必用英语呢?咱都是中国程序员,用中文不好吗?
简单的说就是重载了->操作符的类指针。这样可以防止由于某些原因导致错误的类赋值,加强C++类型检查.
cber 2001-12-28
  • 打赏
  • 举报
回复
I do not agree with this sentense:
It is part of STL (I think). Aotu pointer is different from smart pointer (please pay attention to the ownership of the pointer).

Smart pointer is not part of STL (maybe you can call it is part of SL:)), and auto_ptr<> is also a smart pointer, it is just a simple smart pointer.
Anyway, in boost library, there are 4 smart pointers there (include tangtao's shared_ptr), they all work fine and have more power than auto_ptr provided by SL, I suggest you to use them instead of write your own one.
helloeveryone 2001-12-28
  • 打赏
  • 举报
回复
really ?
tangtao 2001-12-28
  • 打赏
  • 举报
回复
i like use boost::shared_ptr,hehe
qqchen79 2001-12-28
  • 打赏
  • 举报
回复
什么时候大家都开始玩E文的?:)
garfield_82 2001-12-28
  • 打赏
  • 举报
回复
Is do_do that lovely woodpecker?
hedong 2001-12-28
  • 打赏
  • 举报
回复
“poiter” and “reference” is important in C++,
but i can't handle it well.
Last_Dodo 2001-12-28
  • 打赏
  • 举报
回复
Smart pointer is basically a class template that wraps a pointer. It is part of STL (I think). Aotu pointer is different from smart pointer (please pay attention to the ownership of the pointer). Some years back, when we do not have STL libary, I found it is very helpful to write one. The overhead is because you use the wrapper not the pointer itself. For more informat, please read related topic or send mail to david_f_deng@hotmail.com

For question 3, I'd better send you e-mail unless there are too many people interested in this topic.

The last, do do is a catoon character (I have two kids that is why I watch lot of cartoons). It is a bird. For more information please get your own kid :=) (should I say get wife or MM first ^_^ )
zhanghy 2001-12-28
  • 打赏
  • 举报
回复
please search the www.google.com for "Smart Pointer" first, then review the question
ttzzgg_80713 2001-12-28
  • 打赏
  • 举报
回复
cppview.yeah.net
cber 2001-12-28
  • 打赏
  • 举报
回复
if you have time, pls read the source of the header file <memory>, auto_ptr<> is defined there.
linning2570 2001-12-28
  • 打赏
  • 举报
回复
((`'-"``""-'`))
        )  -  - (
       /  (o _ o)         \  ( 0 )  /
       _'-.._'='_..-'_
      /`;#'#'#.-.#'#'#;`      \_))  '#'  ((_/
       #. ☆ ☆ ☆  #
       '#. 元旦快乐.#'
       / '#.   .#'        _\ \'#. .#'/ /_
      (((___) '#' (___)))
heimeng 2001-12-28
  • 打赏
  • 举报
回复
拽~
pinel 2001-12-28
  • 打赏
  • 举报
回复
C++ allows you to create “smart pointer” classes that encapsulate pointers and override pointer operators to add new functionality to pointer operations. Templates allow you to create generic wrappers to encapsulate pointers of almost any type.

The following code outlines a simple reference count garbage collector. The template class Ptr<T> implements a garbage collecting pointer to any class derived from RefCount.

#include <stdio.h>

#define TRACE printf

class RefCount {
int crefs;
public:
RefCount(void) { crefs = 0; }
~RefCount() { TRACE("goodbye(%d)\n", crefs); }
void upcount(void) { ++crefs; TRACE("up to %d\n", crefs);}
void downcount(void)
{
if (--crefs == 0)
{
delete this;
}
else
TRACE("downto %d\n", crefs);
}
};

class Sample : public RefCount {
public:
void doSomething(void) { TRACE("Did something\n");}
};

template <class T> class Ptr {
T* p;
public:
Ptr(T* p_) : p(p_) { p->upcount(); }
~Ptr(void) { p->downcount(); }
operator T*(void) { return p; }
T& operator*(void) { return *p; }
T* operator->(void) { return p; }
Ptr& operator=(Ptr<T> &p_)
{return operator=((T *) p_);}
Ptr& operator=(T* p_) {
p->downcount(); p = p_; p->upcount(); return *this;
}
};

int main() {
Ptr<Sample> p = new Sample; // sample #1
Ptr<Sample> p2 = new Sample; // sample #2
p = p2; // #1 will have 0 crefs, so it is destroyed;
// #2 will have 2 crefs.
p->doSomething();
return 0;
// As p2 and p go out of scope, their destructors call
// downcount. The cref variable of #2 goes to 0, so #2 is
// destroyed
}

Classes RefCount and Ptr<T> together provide a simple garbage collection solution for any class that can afford the int per instance overhead to inherit from RefCount. Note that the primary benefit of using a parametric class like Ptr<T> instead of a more generic class like Ptr is that the former is completely type-safe. The preceding code ensures that a Ptr<T> can be used almost anywhere a T* is used; in contrast, a generic Ptr would only provide implicit conversions to void*.

For example, consider a class used to create and manipulate garbage collected files, symbols, strings, and so forth. From the class template Ptr<T>, the compiler will create template classes Ptr<File>, Ptr<Symbol>, Ptr<String>, and so on, and their member functions: Ptr<File>::~Ptr(), Ptr<File>::operator File*(), Ptr<String>::~Ptr(), Ptr<String>::operator String*(), and so on.

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧