在C++中用括号初始化数组对象
这里有一个类,有两个私有字段x和y在C++中用括号初始化数组对象,c++,arrays,initialization,array-initialization,object-initialization,C++,Arrays,Initialization,Array Initialization,Object Initialization,这里有一个类,有两个私有字段x和y class Point { private: int x, y; public: Point(int = 1,int = 1); void move(int, int); void print() { cout << "X = " << x << ", Y = " << y << endl; } }; 产出 First array X
class Point
{
private:
int x, y;
public:
Point(int = 1,int = 1);
void move(int, int);
void print()
{
cout << "X = " << x << ", Y = " << y << endl;
}
};
产出
First array
X = 10, Y = 1
X = 20, Y = 1
X = 30, Y = 40
Second array
X = 10, Y = 1
X = 20, Y = 1
X = 40, Y = 1
但是,如果我们像下面那样初始化点阵列,输出会很奇怪
Point array2[] = { (10), (20), (30, 40) };
产出
First array
X = 10, Y = 1
X = 20, Y = 1
X = 30, Y = 40
Second array
X = 10, Y = 1
X = 20, Y = 1
X = 40, Y = 1
为什么30,40不适用于点对象的初始化
下面是完整的测试代码
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int = 1,int = 1);
void move(int, int);
void print()
{
cout << "X = " << x << ", Y = " << y << endl;
}
};
Point::Point(int x, int y)
{
cout << "..::Two Parameter Constructor is invoked::..\n";
this->x = x;
this->y = y;
}
void Point::move(int x, int y)
{
this->x = x;
this->y = y;
}
int main()
{
// Point array1[] = { Point(10), Point(20), Point(30, 40) };
// Use parenthesis for object array initialization;
Point array1[] = { (10), (20), { 30, 40 } }; // curly bracket used for two parameter
Point array2[] = { (10), (20), (30, 40) }; // paranthesis used for all objects
cout << "First array" << endl;
for (int i = 0; i < 3; i++)
array1[i].print();
cout << "Second array" << endl;
for (int i = 0; i < 3; i++)
array2[i].print();
return 0;
}
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
First array
X = 10, Y = 1
X = 20, Y = 1
X = 30, Y = 40
Second array
X = 10, Y = 1
X = 20, Y = 1
X = 40, Y = 1
为什么30、40不起作用:
陈述30,40与陈述{30,40}不同,陈述30与{30}也不同
30,40是一个表达式序列,在本例中为整数文本,由分隔,其计算结果为最后一个表达式,即40。然而,{30,40}在所使用的上下文中是一个
为什么30、40不起作用:
陈述30,40与陈述{30,40}不同,陈述30与{30}也不同
30,40是一个表达式序列,在本例中为整数文本,由分隔,其计算结果为最后一个表达式,即40。然而,所用上下文中的{30,40}是一个。编译器将30,40作为一个表达式,并使用逗号运算符计算为单个数字40。您应该打开编译器警告以发现30已被丢弃
数组初始值设定项中的括号表达式被视为表达式,而不是构造函数调用。您可以显式调用构造函数以消除歧义。编译器将30、40作为一个表达式,并使用逗号运算符计算为单个数字40。您应该打开编译器警告以发现30已被丢弃
数组初始值设定项中的括号表达式被视为表达式,而不是构造函数调用。您可以显式调用构造函数以消除歧义。代码中的parenathese会造成混淆。写10时,这并不意味着调用参数为10的构造函数。10变成10,你可以用
Point array1[] = { 10, 20, { 30, 40 } };
第二个数组也是如此
(30, 40)
使用so
变成
{ 10, 20, 40 }
如果要调用双参数构造函数,必须像第一个示例那样将其括起来,或者显式调用构造函数
{ 10, 20, Point(30, 40) }
代码中的parenathese正在给您带来混乱。写10时,这并不意味着调用参数为10的构造函数。10变成10,你可以用
Point array1[] = { 10, 20, { 30, 40 } };
第二个数组也是如此
(30, 40)
使用so
变成
{ 10, 20, 40 }
如果要调用双参数构造函数,必须像第一个示例那样将其括起来,或者显式调用构造函数
{ 10, 20, Point(30, 40) }
可能是因为它将30,40作为一个带有逗号运算符的表达式,该运算符的计算结果为单个数字40。@owacoder no可能对此没有任何说明。这正是正在发生的事情。可能是因为它将30,40作为一个表达式,并带有一个逗号运算符,其计算结果为单个数字40。@owacorder no可能对此没有任何说明。这正是正在发生的事情。