Simulatneous声明/初始化成员变量 [英] Simulatneous declare/initialize member variable

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

问题描述

是否有任何原因导致C ++不利?

允许同时声明和启动会员数据?


当前方式:

------------


class DerivedClass:BaseClass {

{

enum {lengthSV = 16}; // SomeVector的长度

vector< double> SomeVector;


DerivedClass(void):BaseClass(SomeArgument),SomeVector(lengthSV,0.0)

{

//施工派生班级

};

};


更好/更糟糕的方式?

- ---------------


class DerivedClass:BaseClass {

{

enum {lengthSV = 16}; // SomeVector的长度

vector< double> SomeVector(lengthSV);


DerivedClass(void):BaseClass(SomeArgument)

{

//派生类的构造< br $>
};

};

Fred

-

Fred Ma

卡尔顿大学电子学系

加拿大安大略省渥太华

Are there any reasons that would make it bad for C++ to
allow simultaneous declaration and initilization of member data?

Current way:
------------

class DerivedClass : BaseClass {
{
enum { lengthSV=16 }; // Length of SomeVector
vector<double> SomeVector;

DerivedClass( void ) : BaseClass( SomeArgument ), SomeVector(lengthSV,0.0)
{
// Construction of Derived Class
};
};

Better/Worse way?
-----------------

class DerivedClass : BaseClass {
{
enum { lengthSV=16 }; // Length of SomeVector
vector<double> SomeVector(lengthSV);

DerivedClass( void ) : BaseClass( SomeArgument )
{
// Construction of Derived Class
};
};
Fred
--
Fred Ma
Dept. of Electronics, Carleton University
Ottawa, Ontario, Canada

推荐答案

" Fred马" < fm*@doe.carleton.ca>写道...
"Fred Ma" <fm*@doe.carleton.ca> wrote...
是否有任何理由会导致C ++不能同时声明和启动会员数据?

当前的方式:
------------

类DerivedClass:BaseClass {
{
enum {lengthSV = 16}; // SomeVector的长度
向量< double> SomeVector;

DerivedClass(void):BaseClass(SomeArgument),
SomeVector(lengthSV,0.0){
//派生类的构造
};
};

更好/更糟糕的方式?
-----------------

类DerivedClass:BaseClass {
{
enum {lengthSV = 16}; // SomeVector的长度
向量< double> SomeVector(lengthSV);


这不是更好的或者更糟。这是不允许的。

DerivedClass(void):BaseClass(SomeArgument)
{
//派生类的构建
};
};
Are there any reasons that would make it bad for C++ to
allow simultaneous declaration and initilization of member data?

Current way:
------------

class DerivedClass : BaseClass {
{
enum { lengthSV=16 }; // Length of SomeVector
vector<double> SomeVector;

DerivedClass( void ) : BaseClass( SomeArgument ), SomeVector(lengthSV,0.0) {
// Construction of Derived Class
};
};

Better/Worse way?
-----------------

class DerivedClass : BaseClass {
{
enum { lengthSV=16 }; // Length of SomeVector
vector<double> SomeVector(lengthSV);
This is not "better" or "worse". This is simply not allowed.

DerivedClass( void ) : BaseClass( SomeArgument )
{
// Construction of Derived Class
};
};




Victor



Victor


Victor Bazarov写道:
Victor Bazarov wrote:



This is not "better" or "worse". This is simply not allowed.



是的,我知道这是不允许的。代码是否更清晰?如果允许的话,那么
会更紧凑吗? Perhpas有一些原因

是不允许的。


Fred

-

Fred Ma

卡尔顿大学电子学系

加拿大安大略省渥太华


Yes, I know it''s not allowed. Wouldn''t the code be clearer and
more compact if it was allowed? Perhpas there are some reasons
that it isn''t allowed.

Fred
--
Fred Ma
Dept. of Electronics, Carleton University
Ottawa, Ontario, Canada



Fred Ma < fm*@doe.carleton.ca>在留言中写道

新闻:40 *************** @ doe.carleton.ca ...

"Fred Ma" <fm*@doe.carleton.ca> wrote in message
news:40***************@doe.carleton.ca...
有什么理由这会让C ++不能同时声明和启动成员数据吗?
Are there any reasons that would make it bad for C++ to
allow simultaneous declaration and initilization of member data?




你的问题不是特定于向量,它适用于所有数据成员。

例如,你可以有一个int i;作为集体成员的声明,所以你的
问题也将是:使用int i(-3);"

而不是必须存在什么问题在构造函数中设置值?

我认为一个答案可能是不同的构造函数将初始化数据

成员的方式不同,所以单个声明(类型)你指的是)

需要被一个需要不同的东西的构造函数覆盖。使用默认值的构造函数只需

不管它。但是这会使代码变得不那么连贯,因为读者不仅仅需要参考构造函数来确定给所有成员的价值是多少。现在你需要查看构造函数和数据声明,而不是将所有内容放在一个位置,

找出给所有成员的值。


也许这个问题归结为真正需要的东西,而不是必需的,但不是必需的。



Your question is not specific to vectors, it holds for all data members.
For example you can have an "int i;" declaration as a class member, so your
question would also be: what problem could exist with using "int i(-3);"
instead of having to set the value in a constructor?

I think one answer may be that different constructors will initialize data
members differently, so a single declaration (of the type you refer to)
would need to be over-ridden by a constructor that needs something
different. Constructors that are fine with the default value would just
leave it alone. But this would make the code less coherent as readers would
have to refer to more than just the constructor to determine what values are
given to all members. Instead of having everything in a single location,
now you need to look at both the constructor and the data declaration to
find out what values are given to all members.

Maybe the question boils down to what is truly needed against what may be
nice to have but is not essential.


这篇关于Simulatneous声明/初始化成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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