为什么人们使用“新” &安培; "删除"太多了?!! [英] Why people use "new" & "delete" too much?!!

查看:48
本文介绍了为什么人们使用“新” &安培; "删除"太多了?!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到几个源代码,我发现他们几乎只使用新

和删除使他们成为对象的关键词。

我为什么要这样做,因为我知道该对象将在应用程序结束时自行销毁



例如:

班级考试

{

public:

int x;

}


int main(int argc,char ** argv)

{

test * n =新测试;

i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app

for example:
class test
{
public:
int x;
}

int main(int argc, char **argv)
{
test *n= new test;

推荐答案

Medvedev写道:
Medvedev wrote:

i see serveral源代码,我发现他们几乎只使用新

和删除使他们成为对象的关键词。

我为什么要这样做,因为我知道该对象将在应用程序结束时自行销毁



例如:

班级考试

{

public:

int x;

}


int main(int argc,char ** argv)

{

测试* n =新测试;





...

删除n;


返回0;

}

i知道以这种方式创建的对象是在堆中有多少

内存而不是堆栈,但为什么他们总是以这种方式定义对象,为什么

不仅仅是说test n。并且该对象将在程序结束时自行销毁

!而不是使用新也许你会

忘记删除最后
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app

for example:
class test
{
public:
int x;
}

int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;

return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end



几个原因。

1.他们来自Java,他们不知道任何更好的

2.他们在容器中存储多态对象

3.他们需要对象的生命周期超过
$ b的范围$ b它被宣布。

Several reasons.

1. They''re coming from Java and they don''t know any better
2. They''re storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.


7月5日上午11点59分,red floyd< no.spam.h ... @ example.comwrote :
On Jul 5, 11:59 am, red floyd <no.spam.h...@example.comwrote:

Medvedev写道:
Medvedev wrote:

i看到几个源代码,我发现他们几乎只使用新的

和删除让他们反对的关键词。

我为什么要这样做,因为我知道该对象将在应用程序结束时自行销毁


i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app


例如:

class test

{

public:

int x;

}
for example:
class test
{
public:
int x;
}


int main(int argc,char ** argv)

{

test * n =新测试;





...

删除n;
int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;


返回0;

}

i知道以这种方式创建的对象是在堆栈有多少内存而不是堆栈,但为什么它们总是以这种方式定义对象,为什么

不仅仅是说测试n。并且该对象将在程序结束时自行销毁

!而不是使用新也许你会

忘记删除最后
return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end



有几个原因。

1.他们来自Java,他们不知道任何更好的

2.他们在容器中存储多态对象

3.他们需要对象的生命周期超过
$ b的范围$ b它已被宣布。


Several reasons.

1. They''re coming from Java and they don''t know any better
2. They''re storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.



如何在范围结束后使用对象!!

how u can use object after it''s scope ends!!


class CFoo

{

};


CFoo * GetFoo()

{

返回新的CFoo;

}


不过,我同意你的意见,人们过去使用new和delete。我最近看到一个用C ++编写的

类库,它试图成为所有C#''ish并要求你

写代码如:


classa-> InsertItem(new classB(param1));

classa-> InsertItem(new classC(param2));


* STUPID * ...如果您的代码太愚蠢,无法确定应该创建哪种对象(请记住,我可以看到为用户定义执行此操作

对象,但这些都是内部对象),那么为什么你期望你的班级的用户需要一个

用户?


Plus,这会伤害你的表现。


" Medvedev" < 3D ******** @ gmail.comwrote in message

news:95 ********************** ************ @ k30g2000 hse.googlegroups.com ...
class CFoo
{
};

CFoo* GetFoo()
{
return new CFoo;
}

Still, I agree with you, people over use new and delete. I recently saw a
class library written in C++ that tried to be all C#''ish and required you to
write code like:

classa->InsertItem(new classB(param1));
classa->InsertItem(new classC(param2));

*STUPID*... if your code is too stupid to decide what kind of object should
be created (and keep in mind, I could see doing this for user defined
objects, but these were all internal objects), then why would you expect a
user of your class to?

Plus, that hurts performance.

"Medvedev" <3D********@gmail.comwrote in message
news:95**********************************@k30g2000 hse.googlegroups.com...

7月5日上午11:59,red floyd< ; no.spam.h ... @ example.comwrote:
On Jul 5, 11:59 am, red floyd <no.spam.h...@example.comwrote:

> Medvedev写道:
>Medvedev wrote:

i看了几个源代码,我发现他们几乎只使用new

和delete。让他们反对的关键词。

我为什么要这样做,因为我知道该对象将在应用程序结束时自行销毁


i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app


例如:

class test

{

public:

int x;

}
for example:
class test
{
public:
int x;
}


int main(int argc,char ** argv)

{

test * n =新测试;





...

删除n;
int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;


返回0;

}

i知道以这种方式创建的对象是在堆栈有多少内存而不是堆栈,但为什么它们总是以这种方式定义对象,为什么

不仅仅是说测试n。并且该对象将在程序结束时自行销毁

!而不是使用新也许你会

忘记删除最后
return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end


有几个原因。
1。他们来自Java,他们不知道更好
2。他们在容器内存储多态物体
3。他们需要对象的生命周期超出声明它的范围。


Several reasons.

1. They''re coming from Java and they don''t know any better
2. They''re storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.



如何在范围结束后使用对象!


how u can use object after it''s scope ends!!



这篇关于为什么人们使用“新” &安培; &QUOT;删除&QUOT;太多了?!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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