如何使用c ++中的构造函数动态声明对象数组 [英] how to dynamically declare an array of objects with a constructor in c++

查看:158
本文介绍了如何使用c ++中的构造函数动态声明对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以创建一个对象的数组,当对象需要传递给它的构造函数。我想要这样的东西:

I was wondering if it was possible to create an array of objects when the object needs things passed into it for the constructor. I want something like this:

MyClass *myVar;
myVar = new MyClass[num];  // I would like to specify the array size after declaration
int i = 0;
for(i = 0;i < num;i++)
   myVar[i] = new MyClass(0,0);  // I would also like to populate the array with new objects



我知道这个工作: p>

I know that this works:

MyClass *myVar;
myVar = new MyClass[num];

但这只适用于构造函数没有传入的情况。是我想要做的可能吗?如果是这样,我该怎么办?

but this only works when the constructor has nothing passed into it. Is what I am trying to do possible? If so, how do I do it?

编辑:我找到了如何使用数组。这是我怎么做的:

I found out how to do it with using arrays. Here is how I did it:

MyClass **myVar;
myVar = new MyClass *[num];
for(i = 0;i < num;i++)
   myVar[0] = new MyClass(0,0);

我会使用向量等,但我的老师告诉我们尽可能使用基本数组。上面的解决方案我实际上从一些代码我的老师写的。谢谢大家的帮助!

I would use vectors and such but my teacher has told us to use basic arrays whenever possible. The above solution I actually got from some code my teacher wrote. Thank you all for your help!

推荐答案

MyClass *myVar;
myVar = new MyClass[num];

实际上,在这种形式下,你不能调用带参数的构造函数。

Actually in this form you cannot invoke constructor which takes parameter(s). It is not allowed by the language specification.

但是,如果您使用 std :: vector 你可以使用,然后你可以创建一个向量调用非默认构造函数为:

However, if you use std::vector, which I recommend you to use, then you can create a vector calling non-default constructor as:

#include <vector> //header file where std::vector is defined

std::vector<MyClass>  arr(num, MyClass(10,20));

它创建一个 num 每个元素通过调用类的复制构造函数创建,通过 MyClass(10,20)作为参数。

It creates a vector of num elements, each element is created by calling copy-constructor of the class, passing MyClass(10,20) as argument to it.

向量也很好,因为现在你不需要自己管理内存。既不是手动分配,也不是手动重新分配。另外,你可以随时通过调用 arr.size()知道元素的数量。你总是知道矢量包含多少元素。您还可以随时添加元素,只需调用 .push_back()成员函数作为

The vector is also good because now you dont need to manage memory yourself. Neither manual allocation, nor manual deallocation. Plus, you can know the number of elements by calling arr.size() anytime. You always know how many elements the vector contains. You can also add elements anytime, just by calling .push_back() member function as:

arr.push_back(MyClass(20,30)); 

现在你可以访问元素,就像访问数组一样, >

And now you can access elements, just like you access array, i.e by using index:

f(arr[i]); // 0 <= i < arr.size();

此外,您可以使用便于惯用编程的迭代器,使您能够使用< algorithm> 标题为:

Additionally, you can use iterators which facilitate idiomatic programming, enabling you to use various algorithmic functions from <algorithm> header as:

#include <algorithm> //header file where std::for_each is defined

std::for_each(arr.begin(), arr.end(), f);

其中 f MyClass& (或 MyClass const& )类型,取决于您要在 f

where f is function which takes one argument of type MyClass& (or MyClass const &) depending on what you want to do in f.

在C ++ 11中,您可以使用lambda:

In C++11, you can use lambda as:

std::for_each(arr.begin(), arr.end(), [](const MyClass & m)
                                      {
                                           //working with m 
                                      });

这篇关于如何使用c ++中的构造函数动态声明对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆