风格问题 [英] a question of style

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

问题描述

如果我有添加将指向foo的指针添加到私有

std :: list的方法,我应该定义添加类似


1. void add(const foo& f){lst.push_back(& f); $


保持指针不在我的界面之内,或者我应该隐含一个

指针被添加到列表中,就像这样


2. void add(foo * f){lst.push_back(f); }


第一种方法是否会引导用户相信他们的foo实际上是存储的b $ b,这可能会导致列表中的内存值不佳

foo被破坏了吗?或者,只要我使用pre和

的帖子条件,这个讨论就没有用了吗?


Tim Partridge

解决方案

" Tim Partridge" < TJ ****** @ lassar.math.uwaterloo.ca>写道...

如果我有一个添加将指向foo的指针添加到私有
std :: list的方法,我应该定义添加类似

1. void add(const foo& f){lst.push_back(& ;F ); }

保持我的界面指针,


肯定不是。这允许在添加的调用中使用''foo''

类型的临时值,并且你不想存储指向任何

的指针列表中的临时工具。

或者我应该隐含一个
指针被添加到列表中,就像这样

2. void add(foo * f){lst.push_back(f); }


是的,这是票。你当然可以使用引用

非const''foo'':


void add(foo& f){lst.push_back( &安培; F); }


这比第一个好一点,因为它不会让呼叫者暂时使用


第一种方法会引导用户相信他们的foo实际上是存储的,一旦foo被破坏,可能会导致列表中的内存值不好吗?


是的。

或者只要我使用pre和
发布条件,这个讨论是否没什么问题?




我想这取决于你如何设置条件和

验证。


Victor


Tim Partridge写道:

如果我有一个添加将指向foo的指针添加到私有
std :: list的方法,我应该定义添加类似

1. void add(const foo& f){lst.push_back(& ;F ); }

保持指针不在我的界面,或者我应该隐含一个
指针被添加到列表中,如此

2. void add(foo * f){lst.push_back(f);第一种方法会引导用户相信他们的foo实际上是存储的,一旦foo被破坏,可能会导致列表中的内存值不好吗?或者,只要我使用pre和
发布条件,这个讨论就没有意义吗?


这是一个技术问题。答案是选择较弱的东西,而不是更强大的东西。除非你需要一个指针的额外功能,否则更喜欢引用。

接口的唯一区别是指针可能是NULL。


你能推动一个NULL?


另一个答案是syme​​try。当然,''get''或''pop''方法应该返回相同类型的
。他们可以返回NULL,例如对于未找到

的情况吗?


最后,你应该避免列表拥有对象的暗示或者

一个人无法登记堆栈对象。


-

Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces



If I have an "add" method that adds a pointer to a foo to a private
std::list, should I define add like

1. void add( const foo &f ) { lst.push_back( &f ); }

to keep pointers out of my interface, or should I make it implicit that a
pointer is being added to the list, like so

2. void add( foo *f ) { lst.push_back( f ); }

Would the first method lead the user to believe that their foo is actually
being stored, potentially leading to a bad memory value in the list once
the foo is destroyed? Or is this discussion moot as long as I use pre and
post conditions?

Tim Partridge

解决方案

"Tim Partridge" <tj******@lassar.math.uwaterloo.ca> wrote...

If I have an "add" method that adds a pointer to a foo to a private
std::list, should I define add like

1. void add( const foo &f ) { lst.push_back( &f ); }

to keep pointers out of my interface,
Most certainly not. This allows to use temporaries of type ''foo''
in a call to ''add'', and you don''t want to store pointers to any
temporaries in your list.
or should I make it implicit that a
pointer is being added to the list, like so

2. void add( foo *f ) { lst.push_back( f ); }
Yes, that''s the ticket. You could of course, use a reference to
non-const ''foo'':

void add(foo& f) { lst.push_back(&f); }

which is a tad better than the first one because it won''t let
the caller to use a temporary.
Would the first method lead the user to believe that their foo is actually
being stored, potentially leading to a bad memory value in the list once
the foo is destroyed?
Yes.
Or is this discussion moot as long as I use pre and
post conditions?



I suppose it depends on how you set up your conditions and their
verification.

Victor


Tim Partridge wrote:

If I have an "add" method that adds a pointer to a foo to a private
std::list, should I define add like

1. void add( const foo &f ) { lst.push_back( &f ); }

to keep pointers out of my interface, or should I make it implicit that a
pointer is being added to the list, like so

2. void add( foo *f ) { lst.push_back( f ); }

Would the first method lead the user to believe that their foo is actually
being stored, potentially leading to a bad memory value in the list once
the foo is destroyed? Or is this discussion moot as long as I use pre and
post conditions?
This is a technical question. The answer is to prefer weaker things to
stronger ones. Prefer references unless you need a pointer''s extra features.
The only difference at an interface is the pointer could be NULL.

Can you push a NULL?

Another answer is symetry. The ''get'' or ''pop'' methods should, of course,
return the same type. Can they return a NULL, such as for a "not found"
situation?

Finally, you should avoid the implication that the list owns the object, or
that one can''t enlist stack objects.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


Tim Partridge



这篇关于风格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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