样式(或其他?)问题:创建临时调用函数 [英] Style (or otherwise?) question: creating temporary to invoke function

查看:64
本文介绍了样式(或其他?)问题:创建临时调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说你在某些功能中,并且你想完成一个任务x

由一个班级为你提供,A。

您更喜欢哪一种,技术原因是什么? (这个

实际上是静态类函数提供的可能是
的东西,但是这个没有提供一个

case - 必须创建一个实例。)


void f()

{

A a;

ax();


A * p =新A();

p-> x();

删除p;


A()。x();

}

解决方案

jeffc写道:

让我们说你在某个功能中,你想要完成一些为你提供的任务x一个班级,A。
你更喜欢哪一个,并且有技术原因吗?
(实际上这可能是为静态类提供的东西函数,
但在这种情况下不提供 - 必须创建一个实例。)

void f(void){
A a;
ax() ; //好的

A * p = new A();
p-> x();
删除p; //从不!

A()。x(); //最好
}




留下一个参考资料

到一个永远不会再参考的对象

是一个麻烦的邀请。

如果其他程序员(意思是你一段时间)维护f()

没有意识到对象是哪个p点已被删除

并决定添加代码以通过p修改对象,

该函数可能会也可能不会表现出预期的行为

和bug可能很难检测和修复。


jeffc写道:

让我们说你在一些功能中,你想完成一个由班级为你提供的任务x

你更喜欢哪一个,并且有技术原因吗? (这实际上是静态类函数可能提供的那种东西,但是在这种情况下没有提供 - 必须创建一个实例。)

void f()
{
a a;
ax();

很好,一堆栈。

A * p = new A();
p-> x();
删除p;

必须调用new并从堆中分配p,OTT。

A()。x();


简洁明了!


Ian

}



" jeffc" <无**** @ nowhere.com>在消息中写道

news:40 ******** @ news1.prserv.net ...

让我们说你在某些地方功能,你想完成一个由班级A为你提供的任务。
你更喜欢哪一个,并且有技术原因吗?
(这实际上是静态类函数可能提供的东西,但是在这种情况下没有提供 - 必须创建一个实例。)

void f()
{
a a;
ax();

A * p = new A();
p-> x();
删除p;



如果由于某种原因你不能做第一次,请做第二次。

A()。x();



如果由于某种原因你不能做前两个这样的偏执事件。


Ioannis Vranos


Let''s say you''re in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;

A().x();
}

解决方案

jeffc wrote:

Let''s say you''re in some function and you want to accomplish
some task x that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it?
(This is actually the sort of thing that might be provided for
with a static class function,
but one is not provided in this case - an instance must be created.)
void f(void) {
A a;
a.x(); // OK

A* p = new A();
p->x();
delete p; // Never!

A().x(); // Best
}



Leaving a reference lying around
to an object that should never be reference again
is an invitation for trouble.
If some other programmer (meaning you after a while) maintaining f()
doesn''t realize that the object to which p points has been deleted
and decides to add code to modify the object through p,
The function may or may not exhibit the expected behavior
and the bug may be very difficult to detect and fix.


jeffc wrote:

Let''s say you''re in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();
Fine, a on stack.
A* p = new A();
p->x();
delete p;
Has to call new and allocate p from heap, OTT.
A().x();
Fine and concise!

Ian
}



"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...

Let''s say you''re in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;

Do the second if for some reason you can''t do the first.

A().x();


Do this paranoid stuff if for some reason you cannot do the previous two.


Ioannis Vranos


这篇关于样式(或其他?)问题:创建临时调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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