SmartPointer&继承与显式构造函数 [英] SmartPointer & Inheritance & Explicit Constructors

查看:50
本文介绍了SmartPointer&继承与显式构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次感谢大家帮助我清除C ++

混乱。我对副本构造函数和

作业进行了一些认真的阅读,我认为我已经掌握了很好的内存。


好​​吧,我来了跨越Scott Meyer的SmartPtr示例大约10年之前

之前。我喜欢类型转换的模板成员函数来解决

继承问题。


在MSVS 2003上,我得到以下错误,如果我声明构造函数

SmartPtr(T * realPtr = 0);显式,即。


显式SmartPtr(T * realPtr = 0);

SmartPtr.h(33):错误C2520:SmartPtr< T>: :SmartPtr:没有非显式的

构造函数可用于隐式转换

with

[

T = Object < br $>
]



[

T =对象

]

我认为是明确的保险反对愚蠢的错误。

有没有办法解决问题并保持明确?


谢谢...


== = Scott Meyer's ===

模板< class T //智能模板类

类SmartPtr {//指针到T对象

public:

SmartPtr(T * realPtr = 0);

T * operator->()const;

T& ; operator *()const;

模板< class newType //模板函数

运算符SmartPtr< newType>()//隐式转换操作。

{

返回SmartPtr< newType>(指针);

}

Thanks everyone again for contributing to helping me clear C++
confusions. I did some serious reading on copy constructors and
assignments and I think I''ve got a good handle on the memory stuff.

Well, I came across Scott Meyer''s SmartPtr example from some 10 years
ago. I like the template member function for type conversion to solve
inheritance issues.

On MSVS 2003, I get the below error, if I declare the constructor
SmartPtr(T* realPtr = 0); as explicit, ie.

explicit SmartPtr(T* realPtr = 0);
SmartPtr.h(33): error C2520: SmartPtr<T>::SmartPtr: no non-explicit
constructor available for implicit conversion
with
[
T=Object
]
and
[
T=Object
]
I think of explicit as an "insurance" against silly mistakes. Is
there a way to solve the issue and keep explicit?

Thanks...

=== Scott Meyer''s===
template<class T// template class for smart
class SmartPtr { // pointers-to-T objects
public:
SmartPtr(T* realPtr = 0);
T* operator->() const;
T& operator*() const;
template<class newType// template function for
operator SmartPtr<newType>() // implicit conversion ops.
{
return SmartPtr<newType>(pointee);
}

推荐答案

co*******@yahoo.com 写道:

好​​吧,我从大约10年前看到了Scott Meyer的SmartPtr示例

之前。我喜欢模板成员函数进行类型转换来解决

继承问题。
Well, I came across Scott Meyer''s SmartPtr example from some 10 years
ago. I like the template member function for type conversion to solve
inheritance issues.



由于智能指针是垃圾收集的基本前兆,你可以考虑一个完整的垃圾收集器或收集的垃圾
/>
语言。实际上没有必要与继承问题作斗争。更多。


-

飞行青蛙顾问Jon D Harrop博士

科学家OCaml
http://www.ffconsultancy.com/product...ntists/?usenet


5月30日下午4:33,Jon Harrop< j ... @ ffconsultancy.comwrote:
On May 30, 4:33 pm, Jon Harrop <j...@ffconsultancy.comwrote:

coder _... @ yahoo.com写道:
coder_...@yahoo.com wrote:

好​​吧,我从大约10年来看到了Scott Meyer的SmartPtr示例/>
前。我喜欢模板成员函数进行类型转换来解决

继承问题。
Well, I came across Scott Meyer''s SmartPtr example from some 10 years
ago. I like the template member function for type conversion to solve
inheritance issues.


由于智能指针是垃圾收集的基本前兆,你可以认为
可能是全面的收集垃圾收集器或垃圾

语言。
As smart pointers are a rudimentary precursor of garbage collection, you
might consider a full-blown garbage collector or a garbage collected
language.



这取决于智能指针的用途。我通常

使用Boehm收集器进行新项目,但在某些情况下我仍然使用智能

指针:例如管理锁或者

管理线程之间的所有权。在过去,我还使用了

它们来简化事务管理,并手动管理

对象分页(将大部分对象保存在磁盘上,
而不是在内存中。


显然,当你支付
时,不使用Boehm收藏家是愚蠢的,但只是显然,它不是一颗银弹,并且只能取代那些正在使用的智能指针

专门用于内存管理。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientée objet /

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

It depends on what the smart pointers are used for. I normally
use the Boehm collector for new projects, but I still use smart
pointers in some cases: to manage locks, for example, or to
manage ownership between threads. In the past, I''ve also used
them for simplified transaction management, and manually managed
object paging (keeping most of the objects physically on disk,
and not in memory).

Obviously, it''s stupid not to use the Boehm collector when you
can, but just as obviously, it''s not a silver bullet, and can
only replace those smart pointers which were being used
exclusively for memory management.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


James Kanze写道:

....
James Kanze wrote:
....

显然,当你支付
时,不使用Boehm收藏家是愚蠢的,
Obviously, it''s stupid not to use the Boehm collector when you
can,



我发现使用智能指针几乎只需要
开发一个无泄漏的应用程序。圆形指针问题是

通常很容易设计。

I have found that using smart pointers is almost all that is needed to
develop a leak free application. The circular pointers issue is
normally very easy to design around.


...但同样明显的是,它不是一个银弹,并且可以
只替换那些正在使用的智能指针

专门用于内存管理。
... but just as obviously, it''s not a silver bullet, and can
only replace those smart pointers which were being used
exclusively for memory management.


这篇关于SmartPointer&amp;继承与显式构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆