gpt4 book ai didi

c++ - 类的对象数组

转载 作者:行者123 更新时间:2023-11-30 00:41:45 25 4
gpt4 key购买 nike

#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]吗?

最佳答案

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()};

关于c++ - 类的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2941717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com