什么时候使用“新” [英] when to use "new"

查看:73
本文介绍了什么时候使用“新”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rookie c ++问题,但我花了最近5年的时间做Java,每次我创建一个我用过的新对象时,每次都是
。在c ++中我可以创建我的对象

没有它让我有点困惑。


我有一个叫做多项式的类。现在它只是一个小小的类,

只有int变量,一个基本的容器类。我通过一些教程使用它,但是在这个特定的教程中它告诉我要做什么

多项式* first = new polynomial();

但在我找到这个网站之前我只是在做
多项式;

我也在努力通过指针。我理解基础知识,但没有

看到使用它们与我的对象这么快的优势。一种方式比另一种更好吗?

谢谢

Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.

I have a class called polynomial. Its a nothing little class right now,
with just int variables, a basic container class. Im using it as I go
through some tutorials, but in this particular tutorial its telling me to do
polynomial *first = new polynomial();
but before I found this site I was just doing
polynomial first;
Im also struggling through pointers. I understand the basics, but fail to
see the advantage using them with my objects so quickly. Is one way better
than the other?

Thanks

推荐答案

" RV5" < RM ***** @ adelphia.net>在留言中写道

news:6a ******************** @ adelphia.com ...
"Rv5" <rm*****@adelphia.net> wrote in message
news:6a********************@adelphia.com...
菜鸟c ++问题,但我花了最近5年的时间做Java,每次我创建一个我用过的新对象时都会这样做。在c ++中我可以创建我的对象
而且它只会让我感到困惑。

我有一个叫做多项式的类。它现在只是一个小类,只有int变量,一个基本的容器类。我正在使用它,因为我去了一些教程,但在这个特定的教程中,它告诉我
做多项式* first = new polynomial();
这似乎是一个糟糕的建议......但在我找到这个网站之前,我只是先做了多项式;
正确。

我也在努力通过指针。我理解基础知识,但没有看到使用它们与我的对象这么快的优势。一种方式比其他方式更好吗?
Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.

I have a class called polynomial. Its a nothing little class right now,
with just int variables, a basic container class. Im using it as I go
through some tutorials, but in this particular tutorial its telling me to
do
polynomial *first = new polynomial(); This seems likely to be a poor advice... but before I found this site I was just doing
polynomial first; Right.
Im also struggling through pointers. I understand the basics, but fail to
see the advantage using them with my objects so quickly. Is one way
better than the other?



在C ++中,大多数类型应该是值类型,表现和使用类似''int''。


只有在需要共享/访问对象实例时才能复制或不应该复制
的实例才应该分配新的

多个位置,或当对象的生命周期需要明确控制时。


不要使用''new''直到你发现你必须这样做。

我希望这会有所帮助,

Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++<> http://www.brainbench.com


In C++, most types should be value types, behaving and used like ''int''.

Instances only should be allocated with new when they can''t or shouldn''t
be copied, when an object instance needs to be shared/accessed from
multiple locations, or when the lifetime of the object needs to be
explicitly controlled.

Don''t use ''new'' until you find out that you have to.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



" Rv5" < RM ***** @ adelphia.net>在留言中写道

news:6a ******************** @ adelphia.com ...

"Rv5" <rm*****@adelphia.net> wrote in message
news:6a********************@adelphia.com...
菜鸟c ++问题,但我花了最近5年的时间做Java,每次我创建一个我用过的新对象时都会这样做。在c ++中我可以创建我的对象
而且它只会让我感到困惑。

我有一个叫做多项式的类。它现在只是一个小类,只有int变量,一个基本的容器类。我正在使用它,因为我去了一些教程,但在这个特定的教程中它告诉我
做多项式* first = new polynomial();
但在我发现这个之前网站我刚刚做了多项式的多项式;


你可能是对的,教程是错误的。如果它没有新的工作

那么就不要使用它。避免新的线索更清晰的代码(因为

对象的生命周期是有限的)和更有效的代码(对于相同的原因b / b
)。偶尔你会看到这样的代码


{

Wotsit * w = new Wotsit();

...

删除w;

}


这显然是垃圾,因为对象的生命周期与
$完全相同b $ bw是一个自动变量。


{

Wotsit w;

...

}


第二个版本也是异常安全的。

我也在努力通过指针。我理解基础知识,但没有看到使用它们与我的对象这么快的优势。有一种方式比其他方式更好吗?
Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.

I have a class called polynomial. Its a nothing little class right now,
with just int variables, a basic container class. Im using it as I go
through some tutorials, but in this particular tutorial its telling me to
do
polynomial *first = new polynomial();
but before I found this site I was just doing
polynomial first;
It''s likely that you are right and the tutorial is wrong. If it works
without new then don''t use it. Avoiding new leads to clearer code (because
the lifetime of the object is limited) and more efficient code (for the same
reason). Occaisionally you see code like this

{
Wotsit* w = new Wotsit();
...
delete w;
}

That''s clearly rubbish because the object lifetime is exactly the same as if
w was an automatic variable.

{
Wotsit w;
...
}

The second version is exception safe too.
Im also struggling through pointers. I understand the basics, but fail to
see the advantage using them with my objects so quickly. Is one way
better than the other?




再次提出相同的建议,如果你没有看到需要指针就不要使用它们。

它们使用起来很棘手,如果可以,请避免使用它们。但是我觉得很奇怪,如果你一直在编程Java,你就不会觉得有必要了。

Java几乎所有东西都是指针。


john



Same advice again, if you don''t see the need for pointers don''t use them.
They are tricky to use correctly so avoid them if you can. But I find it
surprising that you don''t see the need if you have been programming Java, in
Java almost everything is a pointer.

john


也许你应该了解堆栈是什么以及堆是什么。

本地的内存函数中的变量(或类的方法)在堆栈中是
。当函数运行时,局部变量的内存是在一个称为堆栈的特殊内存区域中分配的。一旦函数

返回,为其局部变量分配的内存立即释放

并且其局部变量的所有值都将丢失。以这种方式构造的类

的实例就像普通变量一样,例如int,

float等。

现在我们得到一个问题。您可以编写以下Java代码:

多项式createNew(){

返回新的多项式();

}

该函数创建一个类Plynomial的新实例,并返回该新实例的

引用。


但是在C ++中,如果你写的是

多项式* createNew(){

多项式pn;

返回& pn;

}


它不起作用。它可能被编译和链接,但实际上它非常危险。 Polynomial的实例是在调用函数
时构造的,但是一旦函数返回就会被销毁。但是,

调用者获取函数

返回的不存在对象的指针,并将其作为有效指针。


我们想要一种方法来创建堆栈以外的新对象,这样即使创建它们的函数返回,我们也可以保存对象。所以我们得到了

堆。我们使用新表达和新表达。在堆中创建新对象 - 另一个特殊内存区域,其中由新表达式创建对象。存在。因此

我们可以用C ++编写以下代码:

多项式* createNew(){

多项式* ppn =新的多项式;

返回ppn;

}

由新表达式分配的内存可以并且只能通过C ++代码中的删除

来释放。所以这里有另一件事:删除你创建的任何东西

by new(在它们没用之后,但在你失去指针之前)。

与Java不同,系统并不清楚新的

在这里创造的无用物品。


Rv5 < RM ***** @ adelphia.net> D'è????¢D ???:6a********************@adelphia.com ..。
Maybe you should understand what the stack is and what the heap is.
The memory for a local variable in a function (or a method of a class) is in
the stack. When the function runs, the memory for local variables are
allocated in a special memory area called the stack. Once the function
returns, the memory allocated for its local variables is free immediately
and all the values of its local variables are lost. An instance of a class
constructed in this way acts exactly like an ordinary variable such as int,
float, etc.
Now we get a problem. You can write the following Java codes:
Polynomial createNew() {
return new Polynomial();
}
the function creates a new instance of class Plynomial and returns the
reference of that new instance.

But in C++, if you write
Polynomial* createNew() {
Polynomial pn;
return &pn;
}

It doesn''t work. It may be compiled and linked but actually it''s very
dangerous. The instance of Polynomial is constructed when the function is
called, but also is destroyed as soon as the function returns. However, the
caller gets the pointer of that nonexistent object returned by the function
and takes it as a valid pointer.

We want a way to create new objects other than in the stack, so that we can
hold the objects even if the functions that create them return. So we get
heap. We use "new expression" to create new objects in the heap - another
special memory area where objects created by "new expressions" exist. Thus
we can write the following codes in C++:
Polynomial* createNew() {
Polynomial* ppn = new Polynomial;
return ppn;
}
Memory allocated by "new expressions" can and only can be freed by "delete"
in the C++ codes. So here comes another thing: delete anything you created
by new (after they are useless, but before you lose the pointers of them).
Unlike in Java, the system doesn''t clear the useless objects created by new
here.

"Rv5" <rm*****@adelphia.net> D′è????¢D???:6a********************@adelphia.com.. .
菜鸟c ++问题,但我花了最近5年的时间做Java,每次我创建一个我用过的新对象时都会这样做。在c ++中我可以创建我的对象
而且它只会让我感到困惑。

我有一个叫做多项式的类。它现在只是一个小类,只有int变量,一个基本的容器类。我正在使用它,因为我去了一些教程,但在这个特定的教程中它告诉我
做多项式* first = new polynomial();
但在我发现这个之前网站我只是首先做多项式;
我也在努力通过指针。我理解基础知识,但没有看到使用它们与我的对象这么快的优势。有一种方式比其他方式好吗?

谢谢
Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.

I have a class called polynomial. Its a nothing little class right now,
with just int variables, a basic container class. Im using it as I go
through some tutorials, but in this particular tutorial its telling me to
do
polynomial *first = new polynomial();
but before I found this site I was just doing
polynomial first;
Im also struggling through pointers. I understand the basics, but fail to
see the advantage using them with my objects so quickly. Is one way
better than the other?

Thanks



这篇关于什么时候使用“新”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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