与构造对象的数组 [英] Array of objects with constructor
本文介绍了与构造对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为类小说
。下面提到我可以声明对象数组:
小说OBJ;
但问题是小说
的构造函数,我想叫的我怎么能做到这一点数组的所有索引。我试着以下,但它不工作。
小说OBJ(I,N)[2];
解决方案
您需要使用一个使用动态分配推迟单个元素的结构的适当的容器中。
的std ::矢量<&新GT; OBJ文件(2,新型(I,N));
I have class named Novel
. I can declare array of objects as mentioned below:
Novel obj;
but problem is Novel
has constructor which I want to be called for all indexes of array how can I do that. I tried following but it does not work.
Novel obj(i,n)[2];
解决方案
You need to use a proper container that uses dynamic allocation to defer construction of individual elements.
std::vector<Novel> objs(2, Novel(i,n));
这篇关于与构造对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文