双重,我; re(r),im(i)vs re = r,im = i [英] double re, im; re(r), im(i) vs re=r, im=i

查看:104
本文介绍了双重,我; re(r),im(i)vs re = r,im = i的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TC ++的第32页上,PL SE是一个复杂类的例子如下:


class complex {

double re,im;

public:

complex(double r,double i){re = r; im = i;}

// ...

};


在TC ++ PL SE的第262页上一个复杂类的例子如下:


class complex {

double re,im;

public:

complex(double r,double i):re(r),im(i){}

// ...

};


我明白在后者中re(r)和im(i)在成员

初始化列表中,而在前者中re = r;和im = i;在构造函数的

函数体中。我不清楚的是暗示

以某种方式做事。同样令人困惑的是re = r和re(r)之间的语法差异。后一种形式在

文本中引入了哪些内容?哪里可以使用?它与re = r有什么不同?我该怎么用英语读入



--STH

解决方案

< blockquote> * Hattuari< su ****** @ setidava.kushan.aa> schriebt:

在TC ++的第32页上,PL SE是一个复杂类的例子,如下所示:

类复杂{
double re,im; <公开:
复杂(双r,双i){re = r; im = i;}
// ...
};

在TC ++ PL SE的第262页上,复杂类的示例如下:

类复杂{
double re,im;
public:
complex(double r,double i):re(r),im(i){}
//
};

我明白在后者中re(r)和im(i)属于成员初始化列表,而在前者re = r;和im = i;在构造函数的
函数体中。


正确。


我不清楚的是以某种方式做事的暗示。同样令人困惑的是re = r和re(r)之间的语法差异。
文本中介绍了后一种形式的位置?


不知道因为我没有文字;大概你可以自己找到




哪里可以使用它?它与re = r有什么不同?我该如何用英语阅读?




FAQ:< url:http://www.parashift.com/c++-faq-lite/ ctors.html#faq-10.6> ;.


常见问题解答中未提及或突出显示:您应该(理想情况下)

永远不会依赖于声明顺序提供一些必要的初始化

命令,因为它是微妙的,容易忘记或误解。这可能

意味着您必须使用赋值而不是初始值设定项。它也可能是
意味着从使用直接成员对象到使用指针进行重新设计,或者甚至更好地使用b $ b来消除对特定初始化顺序的需求。


" Hattuari" <苏****** @ setidava.kushan.aa>在消息中写道

news:y5 ******************** @ speakeasy.net ...

On TC ++ PL SE的第32页是一个复杂类的例子如下:

类复杂{
double re,im;
public:
complex( double r,double i){re = r; im = i;}
// ...
};

在TC ++ PL SE的第262页上,复杂类的示例如下:

类复杂{
double re,im;
public:
complex(double r,double i):re(r),im(i){}
//
};

我明白在后者中re(r)和im(i)属于成员初始化列表,而在前者re = r;和im = i;在构造函数的
函数体中。我不清楚的是以某种方式做事的
含义。同样令人困惑的是re = r和re(r)之间的语法差异。后一种形式在
中引入了哪个文本?哪里可以使用?它与re = r有什么不同?我该如何用英语朗读?

--STH




我没有你的文字所以我可以你不回答任何关于

可能找到的东西的问题。


我会说成员初始化应该总是优先考虑
如果两者都有效,则在构造函数体中进行
赋值。关键点是在构造函数运行之前

,必须以某种方式构造所有成员变量

。如果它们是类,首先运行默认构造函数然后

覆盖赋值可能会变慢。


使用普通的旧数据类型,如double可能还不清楚为什么你会使用

语法re(r)。但是如果你的成员是一个类,它可能需要构造函数

参数:


class Fred

{

Ethyl m_e;

public:

Fred(int blort):m_e(blort){}

};


这解释了类的re(r)语法,但对于普通旧数据,同样的东西如何工作

?如果你知道在C ++中

以下是合法的,那就不奇怪了:


int k(5);


它具有与


int k = 5相同的效果;


那么什么时候不使用成员初始化?当你有一些东西时,比复制构造函数参数更复杂:


class Mabel

{

int m_x;

double m_y;

public:

Mabel(){complex_function_to_set_x_and_y(& m_x,& m_y) ;}

};


在我的代码中,这并不常见。


-

Cy
http:// home.rochester.rr.com/cyhome/


Alf P. Steinbach写道:

* Hattuari< ;苏****** @ setidava.kushan.aa> schriebt:

在TC ++的第32页上,PL SE是一个复杂类的示例,如下所示:


不知道因为我没有文字;大概你可以找到自己吗?


人们可能还会假设我在问这个问题之前就已经看了。或许我只需收听他传递的评论中的含义,即基本类型的所有

实例都是C ++中的对象。

哪里可以用过的?它与re = r有什么不同?我该如何用英语阅读?



常见问题解答:< url:http://www.parashift.com/c++-faq-lite/ctors.html# faq-10.6>。




在我的问题中,似乎没有讨论两种表格之间的区别

。 br />
常见问题解答中未提及或突出显示:您应该(理想情况下)
永远不依赖于声明顺序来提供一些必要的初始化订单,因为它的细微且容易忘记或误解。这可能意味着您必须使用赋值而不是初始值设定项。它可能意味着重新设计从使用直接成员对象到使用指针,或者更好,以消除对特定初始化顺序的需要。




是的,我总是发现这种依赖是令人反感的。有时

它们是必要的,例如当一个子系统依赖于对其初始化数据的引用时,b / b
。有时替代品甚至更糟糕。那时我通常记录依赖关系并继续前进。


On page 32 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};

On page 262 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i):re(r), im(i){}
//...
};

I understand that in the latter the re(r) and im(i) are in the member
initialization list, whereas in the former the re=r; and im=i; are in the
function body of the constructor. What I''m not clear on is the implication
of doing things one way or the other. Also confusing is the difference in
syntax between re=r and re(r). Where is this latter form introduced in the
text? Where can it be used? How is it different from re=r? How should I
read in in English?

--STH

解决方案

* Hattuari <su******@setidava.kushan.aa> schriebt:

On page 32 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};

On page 262 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i):re(r), im(i){}
//...
};

I understand that in the latter the re(r) and im(i) are in the member
initialization list, whereas in the former the re=r; and im=i; are in the
function body of the constructor.
Right.

What I''m not clear on is the implication
of doing things one way or the other. Also confusing is the difference in
syntax between re=r and re(r). Where is this latter form introduced in the
text?
Don''t know since I don''t have the text; presumably you could find that
out yourself?

Where can it be used? How is it different from re=r? How should I
read in in English?



FAQ: <url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6>.

Not mentioned or prominently mentioned in the FAQ: you should (ideally)
never depend on declaration order to provide some required initialization
order, because it''s subtle and easy to forget or misunderstand. This might
mean that you have to use assignment instead of initializers. It might also
mean a redesign from using direct member objects to using pointers, or, even
better, to remove the need for a particular initialization order.


"Hattuari" <su******@setidava.kushan.aa> wrote in message
news:y5********************@speakeasy.net...

On page 32 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};

On page 262 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i):re(r), im(i){}
//...
};

I understand that in the latter the re(r) and im(i) are in the member
initialization list, whereas in the former the re=r; and im=i; are in the
function body of the constructor. What I''m not clear on is the implication of doing things one way or the other. Also confusing is the difference in
syntax between re=r and re(r). Where is this latter form introduced in the text? Where can it be used? How is it different from re=r? How should I
read in in English?

--STH



I don''t have your text so I can''t answer any of your questions about where
things might be found.

I would say that member initialization should always be preferred to
assignment in the constructor body if both will work. The key point is that
before your constructor runs, all member variables must be constructed
somehow. If they are classes, running a default constructor first and then
overriding with an assignment could turn out to be slow.

With plain old data types like double it may not be clear why you would use
the syntax re(r). But if your member is a class, it may need constructor
arguments:

class Fred
{
Ethyl m_e;
public:
Fred(int blort) : m_e(blort) {}
};

That explains the re(r) syntax for classes, but how can the same thing work
for plain old data? It''s not so strange if you know that in C++ the
following is legal:

int k(5);

It has the same effect as

int k = 5;

So when would you not use member initialization? When you have something
more complicated to do than just copying the constructor arguments:

class Mabel
{
int m_x;
double m_y;
public:
Mabel() {complicated_function_to_set_x_and_y(&m_x, &m_y);}
};

In my code this doesn''t come up all that often.

--
Cy
http://home.rochester.rr.com/cyhome/


Alf P. Steinbach wrote:

* Hattuari <su******@setidava.kushan.aa> schriebt:

On page 32 of TC++PL SE is an example of a complex class as follows:


Don''t know since I don''t have the text; presumably you could find that
out yourself?
One might also presume that I looked before asking the question. Perhaps I
should simply glean the implication from his passing comment that all
instances of fundamental types are objects in C++.
Where can it be used? How is it different from re=r? How should I
read in in English?



FAQ: <url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6>.



That doesn''t seem to discuss the difference between the two forms presented
in my question.
Not mentioned or prominently mentioned in the FAQ: you should (ideally)
never depend on declaration order to provide some required initialization
order, because it''s subtle and easy to forget or misunderstand. This
might
mean that you have to use assignment instead of initializers. It might
also mean a redesign from using direct member objects to using pointers,
or, even better, to remove the need for a particular initialization order.



Yes, I''ve always found such dependencies to be objectionable. Sometimes
they are necessary, such as when one subsystem relies on a reference to
another for it''s initialization data. Sometimes the alternatives are even
worse. At that point I usually document the dependencies and move on.


这篇关于双重,我; re(r),im(i)vs re = r,im = i的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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