特定于班级夫妇的代码 [英] Code specific to couples of classes

查看:76
本文介绍了特定于班级夫妇的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会试着澄清一下这个神秘的主题。


假设我有一个带有虚拟方法的基类''GeometricObject''

''双倍距离(const GeometricObject&)const''。


在派生类中我有例如''Segment'',代表一个段
一行的
和'Arc'',代表一个圆弧。


用于计算距离的代码的正确位置
Segment和Arc之间的
?它不是特定的段,也不是特定于Arc,而是两个对象的耦合。


谢谢,


Daniele

解决方案

dl写道:


我会试着澄清一下这个神秘的主题。


假设我有一个带有虚拟方法的基类''GeometricObject''

''双倍距离(const GeometricObject&)const''。


在派生类中我有例如''Segment'',代表一个段
$ b一行的$ b和'Arc'',代表一个圆弧。


用于计算距离的代码的正确位置
$ b段和弧之间的$ b?它不是特定于段,也不是特定于Arc,而是两个对象的耦合。



全局函数怎么样,可能包含在你的类所在的同名

命名空间中。


HTH,

- J.


3月4日18:05,Jacek Dziedzic< jacek.dziedzic.nosp .. 。@ gmail.com>

写道:


一个全局函数怎么样,也许包含在同一个
$ b $中b你的课程所在的命名空间。



谢谢。


是的,它有效。但是它是正确的地方吗?


dl写道:


On 4三月,18:05,亚切克Dziedzic的< jacek.dziedzic.nosp ... @ gmail.com>

写道:

个;一个全局函数怎么样,可能包含在你的类所在的命名空间中。



谢谢。


是的,它有效。但是它是正确的地方吗?



绝对。


如果你想要一些权威的话试试Scott Meyer的第23页

有效的C ++:首选非会员非朋友的功能到会员功能。


-

Alan Johnson


I''ll try to clarify the cryptic subject line.

Let''s say I have a base class ''GeometricObject'' with a virtual method
''double distance (const GeometricObject &) const''.

Among the derived classes I have eg ''Segment'', representing a segment
of a line, and ''Arc'', representing an arc of circle.

What is the right place to put the code for computing the distance
between a Segment and an Arc? It is not specific to the Segment nor to
the Arc, but to the coupling of the two objects.

Thank you,

Daniele

解决方案

dl wrote:

I''ll try to clarify the cryptic subject line.

Let''s say I have a base class ''GeometricObject'' with a virtual method
''double distance (const GeometricObject &) const''.

Among the derived classes I have eg ''Segment'', representing a segment
of a line, and ''Arc'', representing an arc of circle.

What is the right place to put the code for computing the distance
between a Segment and an Arc? It is not specific to the Segment nor to
the Arc, but to the coupling of the two objects.

How about a global function, perhaps contained in the same
namespace your classes are in.

HTH,
- J.


On 4 Mar, 18:05, Jacek Dziedzic <jacek.dziedzic.n.o.s.p....@gmail.com>
wrote:

How about a global function, perhaps contained in the same
namespace your classes are in.

Thank you.

Yes it works. But is it "the right place"?


dl wrote:

On 4 Mar, 18:05, Jacek Dziedzic <jacek.dziedzic.n.o.s.p....@gmail.com>
wrote:

> How about a global function, perhaps contained in the same
namespace your classes are in.


Thank you.

Yes it works. But is it "the right place"?

Absolutely.

If you want some authoritative word on that try Item 23 of Scott Meyer''s
Effective C++: "Prefer non-member non-friend functions to member functions."

--
Alan Johnson


这篇关于特定于班级夫妇的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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