初始化表示法 [英] Initialization notation

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

问题描述

您好,


我对对象初始化有点困惑。根据Stroustrup

(C ++编程语言,特别版,第10.2.3节)构造函数

参数应以下列符号之一提供:


MyClass instance1 = MyClass(1,2,3);

MyClass instance2(4,5,6);


什么很奇怪:第二种符号变体被称为第一种符号变体的缩写形式

。我没有找到任何进一步的参考这个

问题。


但是......第一种符号变体只编译(Microsoft 7.0编译器)如果

Date有一个有效的复制构造函数 - 即使它没有在

表达式中使用。赋值运算符似乎没有必要......


第二种表示法编译时没有复制构造函数和赋值

运算符(都声明为私有而不是定义)。


如果第二种符号确实是第一种变体的缩写,那么

语句应该表现得一样......


那么这里发生了什么?


问候

托马斯

Hello,

I''m a little confused about object initialization. According to Stroustrup
(The C++ Programming Language, Special Edition, Section 10.2.3) constructor
arguments should be supplied in one of the following notations:

MyClass instance1 = MyClass(1, 2, 3);
MyClass instance2(4, 5, 6);

What''s curious: the second notation variant is called an "abbreviated form"
of the first notation variant. I dind''t find any further references to this
issue.

But... the first notation variant only compiles (Microsoft 7.0 compiler) if
Date has a valid copy constructor - even though it isn''t used in the
expression. The assignment operator doesn''t seem to be necessary...

The second notation compiles without copy constructor and assignment
operator (both declared private and not defined).

If the second notation really is an abbreviation of the first variant, both
statements should behave the same way...

So what''s happening here?

Regards
Thomas

推荐答案



" Thomas Lorenz" <螺****** @ gmx.de>在消息中写道

news:Xn *************************** @ 198.120.69.11 ..。

"Thomas Lorenz" <Lo******@gmx.de> wrote in message
news:Xn***************************@198.120.69.11.. .
你好,

我对对象初始化感到困惑。根据Stroustrup
(C ++编程语言,特别版,第10.2.3节),
构造函数参数应以下列符号之一提供:

MyClass instance1 = MyClass( 1,2,3 ;;
MyClass instance2(4,5,6);

但是......第一种符号变体只编译(Microsoft 7.0编译器)
if Date有一个有效的复制构造函数 - 即使它没有在
表达式中使用。赋值运算符似乎没有必要...


标准规定即使复制c''tor被

省略也是如此实施它应该仍然可访问。从理论上讲,MyClass(1,2,3)

会生成一个临时对象,然后将其复制到instance1中,因此需要复制

c''tor。一个实现可以在这里免除复制结构

。如果复制c''tor是私有的,它就不会编译。没有

赋值,因此赋值运算符不会出现。


第二种表示法编译时没有复制构造函数和赋值运算符(两者都声明是私有的而且没有定义。)
Hello,

I''m a little confused about object initialization. According to Stroustrup
(The C++ Programming Language, Special Edition, Section 10.2.3) constructor arguments should be supplied in one of the following notations:

MyClass instance1 = MyClass(1, 2, 3);
MyClass instance2(4, 5, 6);
But... the first notation variant only compiles (Microsoft 7.0 compiler) if Date has a valid copy constructor - even though it isn''t used in the
expression. The assignment operator doesn''t seem to be necessary...
Standard makes this requirement that even if the copy c''tor is elided by
implementation it should still be accessible. Theoretically MyClass(1, 2, 3)
makes a temporary object which is then copied into instance1, hence copy
c''tor is required. An implemenrtation is free to elide the copy construction
here. Still if the copy c''tor is made private it won''t compile. There is no
assignment so assignment operator doesn''t come into picture.

The second notation compiles without copy constructor and assignment
operator (both declared private and not defined).




对,因为没有创建临时工具而且没有复制

也是如此。

-Sharad



Right, becuase no temporaries are being created and there is no copy
construction too.
-Sharad


" Sharad Kala" <无****************** @ yahoo.com>写在

新闻:2n ************ @ uni-berlin.de:
"Sharad Kala" <no******************@yahoo.com> wrote in
news:2n************@uni-berlin.de:

" Thomas Lorenz <螺****** @ gmx.de>在消息中写道
新闻:Xn *************************** @ 198.120.69.11 ..。

"Thomas Lorenz" <Lo******@gmx.de> wrote in message
news:Xn***************************@198.120.69.11.. .
您好,

我对对象初始化感到困惑。根据Stroustrup(The C ++ Programming Language,Special Edition,Section
10.2.3)
Hello,

I''m a little confused about object initialization. According to
Stroustrup (The C++ Programming Language, Special Edition, Section
10.2.3)


构造函数


constructor

参数应该在其中一个中提供以下符号:

MyClass instance1 = MyClass(1,2,3);
MyClass instance2(4,5,6);
arguments should be supplied in one of the following notations:

MyClass instance1 = MyClass(1, 2, 3);
MyClass instance2(4, 5, 6);


但是......第一个符号变体只编译(Microsoft 7.0
编译器)
But... the first notation variant only compiles (Microsoft 7.0
compiler)


如果

Date有一个有效的复制构造函数 - 即使它不是在
表达式中使用。赋值运算符似乎没有必要...
Date has a valid copy constructor - even though it isn''t used in the
expression. The assignment operator doesn''t seem to be necessary...



标准规定,即使复制c''tor被删除,它仍然应该被删除可以访问。从理论上讲,MyClass(1,2,3)创建了一个临时对象,然后将其复制到
instance1中,因此需要复制c''tor。实施可以在这里免除复制结构。仍然如果复制c''tor是私人的,它就不会编译。没有任务,所以分配
操作员没有进入画面。



Standard makes this requirement that even if the copy c''tor is elided
by implementation it should still be accessible. Theoretically
MyClass(1, 2, 3) makes a temporary object which is then copied into
instance1, hence copy c''tor is required. An implemenrtation is free to
elide the copy construction here. Still if the copy c''tor is made
private it won''t compile. There is no assignment so assignment
operator doesn''t come into picture.




嗯......这意味着Stroustrup错了。第二种表示法并不是第一种表示法的缩写,因为它没有对课程施加任何限制

(除了使用的存在)构造函数;)


我真的不明白为什么标准要求复制构造函数......

第一个符号看起来像...


1.)使用默认构造函数创建MyClass实例

2.)使用参数化构造函数创建临时实例

3 。)将2.)的结果分配给1.)


正如我所说 - 这就是_looks_的样子。如果我会写一些荒谬的东西

就像


MyClass instance1(MyClass(1,2,3));


我会看到复制构造函数将如何发挥作用...


所以 - 我错过了关于复制构造函数和赋值的内容

运营商?



Hmmm... this means Stroustrup is wrong. The second notation isn''t really an
abbreviation of th first notation, since it doesn''t impose any restrictions
on the class (except the existance of the used constructor ;)

I really don''t see why the standard demands the copy constructor... The
first notation looks like...

1.) Create an instance of MyClass using the default constructor
2.) Create a temporary instance using the parametrized constructor
3.) Assign the result of 2.) to 1.)

As I said - that''s how it _looks_ like. If I''d write something nonsensical
like

MyClass instance1(MyClass(1,2,3));

I''d see how the copy constructor would come into play...

So - am I missing something about copy constructors and assignment
operators?




" Thomas Lorenz" <螺****** @ gmx.de>在消息中写道

news:Xn *************************** @ 198.120.69.11 ..。

"Thomas Lorenz" <Lo******@gmx.de> wrote in message
news:Xn***************************@198.120.69.11.. .
" Sharad Kala" <无****************** @ yahoo.com>在
新闻中写道:2n ************ @ uni-berlin.de:
嗯...这意味着Stroustrup错了。第二种表示法并不是真正的



我很确定不会是这样的。也许你误解了

上下文。

第一个符号的缩写,因为它没有对该类施加任何
限制(除了存在使用过的构造函数;)

我真的不明白为什么标准要求复制构造函数......
第一个符号看起来像......

1.)使用默认构造函数创建MyClass实例


不正确。我们首先使用构造函数使用

三个整数作为参数。事实上编译器没有为你生成默认的c''tor

在这种情况下。

2.)使用参数化构造函数创建一个临时实例


这是第1步。

3.)将结果分配给2.)到


不,有没有任务。从理论上讲,临时创建的是使用copy c''tor复制到instance1中的



正如我所说 - 那就是_looks_之类的。如果我写了一些荒谬的东西,比如

MyClass instance1(MyClass(1,2,3));

我会看到副本怎么样构造函数会发挥作用...

所以 - 我错过了关于复制构造函数和赋值运算符的东西吗?
"Sharad Kala" <no******************@yahoo.com> wrote in
news:2n************@uni-berlin.de: Hmmm... this means Stroustrup is wrong. The second notation isn''t really an

I am damn sure that won''t be the case. Perhaps you are misinterpreting the
context.
abbreviation of th first notation, since it doesn''t impose any restrictions on the class (except the existance of the used constructor ;)

I really don''t see why the standard demands the copy constructor... The
first notation looks like...

1.) Create an instance of MyClass using the default constructor
Incorrect. We are first making the temporary using the constructor taking
three ints as parameter. In fact compiler generates no default c''tor for you
in this case.
2.) Create a temporary instance using the parametrized constructor
That''s step 1.
3.) Assign the result of 2.) to 1.)
Nope, there is no assignment. Theoretically the temporary created is then
copied into instance1 using the copy c''tor.
As I said - that''s how it _looks_ like. If I''d write something nonsensical
like

MyClass instance1(MyClass(1,2,3));

I''d see how the copy constructor would come into play...

So - am I missing something about copy constructors and assignment
operators?




也许那,

someclass c1 = c2; //没有任务,调用副本c''tor

c2 = c3; //作业


-Sharad



Perhaps that,
someclass c1 = c2; // No assignment, invokes copy c''tor
c2 = c3; // Assignment

-Sharad


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

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