ABC中的分配操作员 [英] Assignment Operator in ABC

查看:66
本文介绍了ABC中的分配操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有char *成员的抽象基类。这个抽象基类是否需要

赋值运算符?为什么或

为什么不呢?


提前致谢。


Ken Wilson

阿梅尔。 DLX。 Tele,Gary Moore LP,LP褪色DC,

Jeff Beck Strat,Morgan OM Acoustic,

Rick 360/12,Std。 Strat(MIM),Mesa 100 Nomad,

Mesa F-30

I have an abstract base class that has char* members. Is an
assignment operator necessary for this abstract base class? Why or
why not?

Thanks in advance.

Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP Faded DC,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."

推荐答案

2004年5月25日星期二00:44:12 GMT,Rick N. Backer< ke ******** @ NsOhSaPw.cAaM>

写道:
On Tue, 25 May 2004 00:44:12 GMT, Rick N. Backer <ke********@NsOhSaPw.cAaM>
wrote:
我有一个有char *成员的抽象基类。这个抽象基类是否需要
赋值运算符?为什么或为什么不呢?


这取决于你是否希望允许分配具体子类的

对象。赋值运算符永远不会是b $ b必要的如果你不想支持任务。


如果你/希望/希望支持派生类对象的分配,并且

你不要为ABC写一个赋值运算符,然后就像任何基类的情况一样,编译器将生成一个执行

浅拷贝的运算符(它将字面上复制您的指针值)。如果这些值代表动态分配的内存

并且你没有对它们进行任何花哨的引用计数,这可能不是你想要的。

IOW,你的BC是A的事实并没有保证基本的三个规则从

申请。

-leor

提前致谢。

Ken Wilson

Amer。 DLX。 Tele,Gary Moore LP,LP Faded DC,Jeff Beck Strat,Morgan OM Acoustic,
Rick 360/12,Std。 Strat(MIM),Mesa 100 Nomad,
Mesa F-30

Goodnight Austin,Texas,无论你身在何处。
I have an abstract base class that has char* members. Is an
assignment operator necessary for this abstract base class? Why or
why not?
That rather depends on whether or not you wish to allow assignment of
objects of concrete subclasses or not. An assignment operator is never
"necessary" if you don''t wish to support assignment.

If you /do/ wish to support assignment of the derived class objects, and
you don''t write an assignment operator for the ABC, then just as would be
the case for any base class, the compiler will generate one that does a
shallow copy (it will literally copy your pointer values). That''s probably
not what you want if those values represent dynamically allocated memory
and you''re not doing any fancy reference counting on them.

IOW, the fact your BC is A doesn''t keep the basic Rule of Three from
applying.
-leor

Thanks in advance.

Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP Faded DC,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."




-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt .html


" Rick N. Backer" <柯******** @ NsOhSaPw.cAaM>写道...
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote...
我有一个有char *成员的抽象基类。这个抽象基类是否需要
赋值运算符?为什么或
为什么不呢?
I have an abstract base class that has char* members. Is an
assignment operator necessary for this abstract base class? Why or
why not?




信息不足。阅读三条规则。


Victor



Not enough information. Read up on "Rule of Three".

Victor


2004年5月24日星期一21:41:33 - 0400,Leor Zolman< le ** @ bdsoft.com>确实

勇敢地宣称:
On Mon, 24 May 2004 21:41:33 -0400, Leor Zolman <le**@bdsoft.com> did
courageously avow:
2004年5月25日星期二00:44:12 GMT,Rick N. Backer< ke ****** **@NsOhSaPw.cAaM>
写道:
On Tue, 25 May 2004 00:44:12 GMT, Rick N. Backer <ke********@NsOhSaPw.cAaM>
wrote:
我有一个有char *成员的抽象基类。这个抽象基类是否需要
赋值运算符?为什么或为什么不呢?
这取决于你是否希望允许分配具体子类的对象。赋值运算符永远不是必要的。如果你不想支持任务。

如果你/希望/希望支持派生类对象的分配,并且你没有写一个赋值运算符ABC,然后就像任何基类的情况一样,编译器将生成一个执行浅层复制(它将逐字复制指针值)。如果这些值代表动态分配的内存并且你没有对它们进行任何花哨的引用计数,那可能不是你想要的。
I have an abstract base class that has char* members. Is an
assignment operator necessary for this abstract base class? Why or
why not?
That rather depends on whether or not you wish to allow assignment of
objects of concrete subclasses or not. An assignment operator is never
"necessary" if you don''t wish to support assignment.

If you /do/ wish to support assignment of the derived class objects, and
you don''t write an assignment operator for the ABC, then just as would be
the case for any base class, the compiler will generate one that does a
shallow copy (it will literally copy your pointer values). That''s probably
not what you want if those values represent dynamically allocated memory
and you''re not doing any fancy reference counting on them.




感谢您的投入。我添加了赋值运算符。我需要深度复制
。我应该将它声明为虚拟的

函数吗?

IOW,你的BC是A的事实并没有保持基本的三法则来自



我已经访问了C ++ FAQ-Lite,但找不到对规则的引用

of Three。我是否可以安全地假设您指的是不能继承,构造函数,析构函数和

赋值的三个

函数?或者你指的是更神秘的巫术? :-) -leor



Thank you for your input. I have added the assignment operator. I
needed the deep copying. Should I be declaring it as a virtual
function?

IOW, the fact your BC is A doesn''t keep the basic Rule of Three from
applying.
I''ve visited the C++ FAQ-Lite and cannot find a reference to the Rule
of Three. Am I safe in assuming you are referring to the three
functions that don''t get inherited, constructor, destructor and
assignment? Or are you referring to even more arcane wizardry? :-) -leor



Ken Wilson

只是因为人们不理解你并不代表

您是艺术家


Ken Wilson
"Just because people don''t understand you doesn''t mean
you are an artist"


这篇关于ABC中的分配操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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