#include<iostream.h>
class test{
int a;
char b;
public:
test()
{
cout<<"\n\nDefault constructor being called";
}
test(int i,char j)
{
a=i;
b=j;
cout<<"\n\nConstructor with arguments called";
}
};
int main()
{
test tarray[5];
test newobj(31,'z');
};
在上面的代码段中,我们可以将值初始化为tarray[5]
吗?
答案 0 :(得分:7)
test tarray[5] = {test(1, 2), test(), test(5, 6), test()};
第五个元素将使用默认构造函数初始化。
//here length of array will be inferred from number of initializers,
// so it's going to be 4
test tarray[] = {test(1, 'a'), test(), test(5, 'b'), test()};