*新的还是新的? [英] *new or new?

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

问题描述

更好:

AnyClass& reg = * new AnyClass();

reg.Function();


或:

AnyClass * reg = new AnyClass();

reg-> Function();


谢谢

克里斯蒂亚诺

It is better:
AnyClass &reg=*new AnyClass();
reg.Function();

or:
AnyClass *reg=new AnyClass();
reg->Function();

Thanks
Cristiano

推荐答案

" Cristiano" < CR ********** @ NSquipo.it>写道...
"Cristiano" <cr**********@NSquipo.it> wrote...
这是更好的:
AnyClass& reg = * new AnyClass();
reg.Function();

或:
AnyClass * reg = new AnyClass();
reg-> Function();
It is better:
AnyClass &reg=*new AnyClass();
reg.Function();

or:
AnyClass *reg=new AnyClass();
reg->Function();




对于这两个片段,区别仅在于在该程序使用的存储量

。后一种情况_allocates_一些

存储指针,对于前者它未指定

因为它取决于实现是否引用了

需要存储。


Victor



For those two fragments the difference is only in the amount
of storage the program uses. The latter case _allocates_ some
storage for the pointer, and for the former it is unspecified
because it is up to the implementation whether references do
require storage.

Victor




" Cristiano" < CR ********** @ NSquipo.it> skrev i en meddelelse

新闻:P1 ****************** @ news3.tin.it ...

"Cristiano" <cr**********@NSquipo.it> skrev i en meddelelse
news:P1******************@news3.tin.it...
它更好:
AnyClass& reg = * new AnyClass();
reg.Function();

或:
AnyClass * reg = new AnyClass( );
reg->功能();

谢谢
克里斯蒂亚诺
It is better:
AnyClass &reg=*new AnyClass();
reg.Function();

or:
AnyClass *reg=new AnyClass();
reg->Function();

Thanks
Cristiano




第一个形式模糊不清。之后如何释放记忆?也许

你很困惑,以前用Java(或C#)编程。在这种情况下,

替代方案是:

AnyClass reg;

reg.Function();


或:


AnyClass * reg = new AnyClass();

reg->功能();


如果这是你的意思,你应该总是更喜欢第一种情况,除非

被迫做其他事情(然后你应该更喜欢智能指针而不是
而不是原始的)。


亲切的问候

Peter



The first form is obscure. How do you free the memory afterwards? Perhaps
you are confused, having programmed in Java (or C#) before. In this case the
alternatives are:
AnyClass reg;
reg.Function();

or:

AnyClass *reg=new AnyClass();
reg->Function();

If this is what you meant, You should always prefer the first case unless
compelled to do otherwise (and then you should prefer a smart pointer rather
than a raw one).

Kind regards
Peter


Peter Koch Larsen写道:
Peter Koch Larsen wrote:
克里斯蒂亚诺 < CR ********** @ NSquipo.it> skrev i en meddelelse
新闻:P1 ****************** @ news3.tin.it ...
"Cristiano" <cr**********@NSquipo.it> skrev i en meddelelse
news:P1******************@news3.tin.it...
这是更好的:
AnyClass& reg = * new AnyClass();
reg.Function();

或:
AnyClass * reg = new AnyClass();
reg-> Function();
第一种形式是模糊的。你怎么在事后释放记忆?
也许你很困惑,[...]
It is better:
AnyClass &reg=*new AnyClass();
reg.Function();

or:
AnyClass *reg=new AnyClass();
reg->Function();
The first form is obscure. How do you free the memory afterwards?
Perhaps you are confused, [...]




困惑!?要释放记忆,你只需要删除& reg。

在这种情况下,替代方案是:

AnyClass reg;
reg.Function();


但这个表格被分配到堆栈中(通常不会太大)。

或:

AnyClass * reg = new AnyClass();
reg-> Function();



Confused!? To free the memory you just need delete &reg.
In this case the alternatives are:

AnyClass reg;
reg.Function();
But this form is allocated into the stack (which usually is not too big).
or:

AnyClass *reg=new AnyClass();
reg->Function();




这是我上面写的,它是我喜欢的形式。< br $>

亲切的问候

克里斯蒂亚诺



Which is what I wrote above and it is the form that I prefer.

Kind regards
Cristiano


这篇关于*新的还是新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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