什么时候应该使用“新”? [英] When should use "new"?

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

问题描述

对于一个类,MyClass,有两种实例化对象的方法:


1. MyClass * MC = new MyClass();


2. MyClass MC;


一般来说,什么时候应该使用1和2?什么是

使用它们的优点/缺点?


谢谢!

解决方案



" garyolsen" < GA ******* @ yahoo.com>在留言新闻中写道:l6 ********************** @ bgtnsc05-news.ops.worldnet.att.net ...

对于一个类,MyClass,有两种实例化对象的方法:

1. MyClass * MC = new MyClass();

2. MyClass MC;

一般来说,什么时候应该使用1和2?什么是使用其中的优点/缺点?




当#2不够时使用#1。


" garyolsen" < GA ******* @ yahoo.com>在留言中写道

新闻:l6 ********************** @ bgtnsc05-news.ops.worldnet.att.net .. 。

对于一个类MyClass,有两种实例化对象的方法:

1. MyClass * MC = new MyClass();
2. MyClass MC;

一般来说,什么时候应该使用1和2?


一般情况下,使用第二种形式,必要时使用第一种形式。

什么是使用其中的优点/缺点?




我能想到的第二种形式

的唯一缺点是它可能不足以完成特定任务。


A *可能*第一种形式的缺点是它可能*

表现较差。另一个是它需要你

记得在完成它时删除它。


-Mike


Ron Natalie< ro*@sensor.com>在消息中写道

新闻:3f ********************** @ news.newshosting.com ...


garyolsen < GA ******* @ yahoo.com>在消息中写道



新闻:l6 ********************** @ bgtnsc05-news.ops.worldnet.att .net ...

对于一个类MyClass,有两种实例化对象的方法:

1. MyClass * MC =新的MyClass();

2. MyClass MC;

一般来说,什么时候应该使用1和2?什么是使用它们的优点/缺点?



当#2不够时使用#1。


你是否想澄清一下#2何时不够?谢谢!


For a class, MyClass, there are two ways to instantiate an object:

1. MyClass *MC = new MyClass();

2. MyClass MC;

In general, when should you have to use 1 and when 2? What''re
advantages/disadvantages of using either?

Thanks!

解决方案


"garyolsen" <ga*******@yahoo.com> wrote in message news:l6**********************@bgtnsc05-news.ops.worldnet.att.net...

For a class, MyClass, there are two ways to instantiate an object:

1. MyClass *MC = new MyClass();

2. MyClass MC;

In general, when should you have to use 1 and when 2? What''re
advantages/disadvantages of using either?



Use #1 when #2 isn''t enough.


"garyolsen" <ga*******@yahoo.com> wrote in message
news:l6**********************@bgtnsc05-news.ops.worldnet.att.net...

For a class, MyClass, there are two ways to instantiate an object:

1. MyClass *MC = new MyClass();

2. MyClass MC;

In general, when should you have to use 1 and when 2?
In general, use the second form, use the first when you must.
What''re
advantages/disadvantages of using either?



The only disadvantage I can think of for the second form
is that it might not be sufficient for a particular task.

A *possible* disadvantage of the first form is that it *might*
have poorer performance. Another is that it requires you to
remember to ''delete'' it when done with it.

-Mike


Ron Natalie <ro*@sensor.com> wrote in message
news:3f**********************@news.newshosting.com ...


"garyolsen" <ga*******@yahoo.com> wrote in message


news:l6**********************@bgtnsc05-news.ops.worldnet.att.net...

For a class, MyClass, there are two ways to instantiate an object:

1. MyClass *MC = new MyClass();

2. MyClass MC;

In general, when should you have to use 1 and when 2? What''re
advantages/disadvantages of using either?



Use #1 when #2 isn''t enough.


Would you mind to clarify a little bit about when #2 isn''t enough? Thanks!


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

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