64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class A
{
public:
A() : x(0), y(0){cout <<0<<0<<"缺省构造函数" <<endl;}
//A(int a, int b) : x(a), y(b){cout <<"普通构造函数" <<endl;}
A(int a = 0, int b = 0) : x(a), y(b){cout <<a<<b<<"普通构造函数" <<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;
}
class A
{
public:
A() : x(0), y(0){cout < <"缺省构造函数" < <endl;}
A(int a, int b) : x(a), y(b){cout < <"普通构造函数" < <endl;}
A(const A &other) : x(other.x), y(other.y){cout < <"拷贝构造函数" < <endl;}
private:
int x;
int y;
};
class A
{
public:
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;
};