类对象初始化 [英] class object initialisation

查看:131
本文介绍了类对象初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一直被教导使用inialization列表来初始化数据

班级成员。我意识到初始化基元和指针使用

一个inialization列表与一个赋值完全相同,但对于类

类型它有不同的效果 - 它调用复制构造函数。


我的问题是什么时候不使用初始化列表初始化数据

班级成员?

问候

Adi


---

外发邮件已通过无病毒验证。

由AVG反查病毒系统( http://www.grisoft.com)

版本:6.0.516 /病毒库:313 - 发布日期:2003年1月1日

Hi,

I have always been taught to use an inialization list for initialising data
members of a class. I realize that initialsizing primitives and pointers use
an inialization list is exactly the same as an assignment, but for class
types it has a different effect - it calls the copy constructor.

My question is when to not use an initalisation list for initialising data
members of a class?
Regards
Adi

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 1/09/2003

推荐答案

" A" < A@iprimus.com.au>在留言中写道

新闻:3f ******** @ news.iprimus.com.au ...

|我一直被教导使用一个初始化列表进行初始化

数据

|班级成员。我意识到初始化原语和指针

使用

| inialization列表与赋值完全相同,但对于类

|类型它有不同的效果 - 它调用复制构造函数。

|

|我的问题是何时不使用初始化列表来初始化数据

|班级成员?


只有当你无法做到时,例如当一个涉及

的复杂计算需要几个中间变量而且一个专用函数不需要b $ b bb是有意义的。


IMO初始化列表应始终是首选。

问候,

-
http://ivan.vecerina.com
"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
| I have always been taught to use an inialization list for initialising
data
| members of a class. I realize that initialsizing primitives and pointers
use
| an inialization list is exactly the same as an assignment, but for class
| types it has a different effect - it calls the copy constructor.
|
| My question is when to not use an initalisation list for initialising data
| members of a class?

Only when you are unable to, e.g. when a complex computation involving
several intermediate variables is needed and a dedicated function doesn''t
make sense.

IMO the initialization list should always be preferred.
Regards,
--
http://ivan.vecerina.com




" A" < A@iprimus.com.au>在消息中写道

新闻:3f ******** @ news.iprimus.com.au ...

"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...

我一直被教导使用inialization列表来初始化一个类的
数据成员。我意识到初始化基元和指针
使用一个inialization列表与一个赋值完全相同,但对于类
类型它有不同的效果 - 它调用复制构造函数。
我的问题是什么时候不使用初始化列表初始化数据
一个类的成员?
Hi,

I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the same as an assignment, but for class
types it has a different effect - it calls the copy constructor.

My question is when to not use an initalisation list for initialising data
members of a class?




基本的经验法则是任何可以去的东西在初始化列表中

应该去那里。显然,你不能把这样的东西放在那里:

if(a)

i = 2;

else

i = 4;



The basic rule of thumb is that anything that can go in the initialize list
should go there. Obviously, you can''t put something like this in there:
if (a)
i = 2;
else
i = 4;


> >

我一直被教导使用一个inialization列表来初始化数据

I have always been taught to use an inialization list for initialising data
一个类的成员。我意识到初始化基元和指针
members of a class. I realize that initialsizing primitives and pointers


使用

一个inialization列表与一个赋值完全相同,但对于类
类型它有不同的效果 - 它调用复制构造函数。

我的问题是什么时候不使用初始化列表初始化一个类的
数据成员?
an inialization list is exactly the same as an assignment, but for class
types it has a different effect - it calls the copy constructor.

My question is when to not use an initalisation list for initialising data members of a class?



基本规则拇指是任何可以进入初始化



The basic rule of thumb is that anything that can go in the initialize



列表的东西都应该去那里。显然,你不能把这样的东西放在那里:
如果(a)
我= 2;
其他
我= 4;


list should go there. Obviously, you can''t put something like this in there:
if (a)
i = 2;
else
i = 4;



很多(如果不是大多数)这些案例都可以被?:运算符覆盖:

MyClass(int a):i(a?2:4){ }


/ Erik



Many, if not most, of those cases can be covered by the ?: operator:
MyClass(int a) : i(a ? 2 : 4) {}

/ Erik


这篇关于类对象初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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