数组对象初始化问题
问题在注释处,
谢谢大家!
#include<iostream>
using namespace std;
class A
{
public:
A() : x(0), y(0){cout<<"缺省构造函数"<<endl;}
A(int a, int b) : x(a), y(b){cout<<"普通构造函数"<<endl;}
//A(int a = 0, int b = 0) : x(a), y(b){cout<<"普通构造函数"<<endl;}//此构造函数与上面两个构造函数等价吗?
A(const A &other) : x(other.x), y(other.y){cout<<"拷贝构造函数"<<endl;}
private:
int x;
int y;
};
void test()
{
A objarray1[2] = /**此处会报错,但构造函数若该为注释掉的有缺省参数的构造函数则不会报错,怎么回事?**/
{
(1, 2),
(3, 4)
};
A objarray2[2] =
{
A(5, 6),
A(7, 8)
};
}
int main()
{
test();
system("pause");
return 0;
}
谢谢大家!
#include<iostream>
using namespace std;
class A
{
public:
A() : x(0), y(0){cout<<"缺省构造函数"<<endl;}
A(int a, int b) : x(a), y(b){cout<<"普通构造函数"<<endl;}
//A(int a = 0, int b = 0) : x(a), y(b){cout<<"普通构造函数"<<endl;}//此构造函数与上面两个构造函数等价吗?
A(const A &other) : x(other.x), y(other.y){cout<<"拷贝构造函数"<<endl;}
private:
int x;
int y;
};
void test()
{
A objarray1[2] = /**此处会报错,但构造函数若该为注释掉的有缺省参数的构造函数则不会报错,怎么回事?**/
{
(1, 2),
(3, 4)
};
A objarray2[2] =
{
A(5, 6),
A(7, 8)
};
}
int main()
{
test();
system("pause");
return 0;
}
...全文


