如果在一行中声明了两个对象,它们的构造顺序是什么?
If two objects are declared in a single line, in which order are they constructed?
假设 class 被定义为
class A {
//.....
};
现在我正在创建两个对象
A a,b;
a
和 b
的创建顺序是什么?是标准定义的吗?
a 将首先创建,然后 b.
在这种情况下,逗号 将用作分隔符 而不是运算符。
例如来自维基百科:
/**
* Commas act as separators in this line, not as an operator.
* Results: a=1, b=2, c=3, i=0
*/
int a=1, b=2, c=3, i=0;
顺序为书面顺序,从左到右。此外,它不是逗号运算符,而只是声明符列表。当使用用户定义的逗号运算符时,顺序实际上是未指定的。
见comma operator and declarators。
来自 8 个声明者 [dcl.decl] 3:
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.
接着说
A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single
declarator. That is
T D1, D2, ... Dn;
is usually equivalent to
T D1; T D2; ... T Dn;
where T
is a decl-specifier-seq and each Di
is an init-declarator. An exception occurs when a name introduced by one of
the declarators hides a type name used by the decl-specifiers, so that when the same decl-specifiers are used in a subsequent
declaration, they do not have the same meaning.
你可以说它们是从左到右构造的。
C++ 规范第 8 章 [[=22=]],说:
Each init-declarator in a declaration is analyzed separately as if it
was in a declaration by itself. (100)
脚注 (100) 继续说:
(100) A declaration with several declarators is usually equivalent to the
corresponding sequence of declarations each with a single declarator.
That is
T D1, D2, ... Dn;
is usually equivalent to
T D1; T D2; ... T Dn;
...然后列出一些例外情况,none 其中适用于这种简单的情况。
所以你的问题的答案是对象是按照你列出它们的顺序构造的。不,它不是逗号运算符。
标准:
Declarators [dcl.decl]:
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.
示例:
class A {
public:
A(std::string const &s): name(s)
{
std::cout << "I am " << name << '\n';
}
std::string name;
};
auto main() -> int
{
A a("a"), b("b");
}
输出:
I am a
I am b
假设 class 被定义为
class A {
//.....
};
现在我正在创建两个对象
A a,b;
a
和 b
的创建顺序是什么?是标准定义的吗?
a 将首先创建,然后 b.
在这种情况下,逗号 将用作分隔符 而不是运算符。
例如来自维基百科:
/**
* Commas act as separators in this line, not as an operator.
* Results: a=1, b=2, c=3, i=0
*/
int a=1, b=2, c=3, i=0;
顺序为书面顺序,从左到右。此外,它不是逗号运算符,而只是声明符列表。当使用用户定义的逗号运算符时,顺序实际上是未指定的。
见comma operator and declarators。
来自 8 个声明者 [dcl.decl] 3:
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.
接着说
A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is
T D1, D2, ... Dn;
is usually equivalent toT D1; T D2; ... T Dn;
whereT
is a decl-specifier-seq and eachDi
is an init-declarator. An exception occurs when a name introduced by one of the declarators hides a type name used by the decl-specifiers, so that when the same decl-specifiers are used in a subsequent declaration, they do not have the same meaning.
你可以说它们是从左到右构造的。
C++ 规范第 8 章 [[=22=]],说:
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself. (100)
脚注 (100) 继续说:
(100) A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is
T D1, D2, ... Dn;
is usually equivalent to
T D1; T D2; ... T Dn;
...然后列出一些例外情况,none 其中适用于这种简单的情况。
所以你的问题的答案是对象是按照你列出它们的顺序构造的。不,它不是逗号运算符。
标准:
Declarators [dcl.decl]:
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.
示例:
class A {
public:
A(std::string const &s): name(s)
{
std::cout << "I am " << name << '\n';
}
std::string name;
};
auto main() -> int
{
A a("a"), b("b");
}
输出:
I am a
I am b