在 C++ 中创建动态对象的动态数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20303820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creation of Dynamic Array of Dynamic Objects in C++
提问by lakesh
I know how to create a array of dynamic objects.
我知道如何创建一组动态对象。
For example, the class name is Stock.
例如,类名是 Stock。
Stock *stockArray[4];
for(int i = 0 ; i < 4;i++)
{
stockArray[i] = new Stock();
}
How do you change this to dynamic array of dynamic objects?
您如何将其更改为动态对象的动态数组?
What I tried:
我试过的:
Stock stockArrayPointer = new Stockstock[4];
股票stockArrayPointer = 新股票[4];
It doesn't work and the error is "The value of Stock** cannot be used to initalize an entity of type Stock.
它不起作用,错误是“Stock** 的值不能用于初始化 Stock 类型的实体。
Second question is after the creation of dynamic array of dynamic objects, what is the syntax to access the pointers in the array.
第二个问题是在创建动态对象的动态数组之后,访问数组中的指针的语法是什么。
Now, I use stockArray[i] = new Stock(); How will this change?
现在,我使用 stockArray[i] = new Stock(); 这将如何改变?
Need some guidance on this...
需要一些指导...
回答by akaltar
If you are using c++ then you shouldn't reinvent the wheel, just use vectors:
如果您使用的是 C++,那么您不应该重新发明轮子,只需使用向量:
#include <vector>
std::vector< std::vector< Stock > > StockVector;
// do this as many times as you wish
StockVector.push_back( std::vector< Stock >() );
// Now you are adding a stock to the i-th stockarray
StockVector[i].push_back( Stock() );
Edit:
编辑:
I didn't understand your question, if you just want to have and array of arrays allocated on the heap just use:
我不明白你的问题,如果你只想在堆上分配数组,只需使用:
Stock** StockArrayArray = new Stock*[n]; // where n is number of arrays to create
for( int i = 0; i < n; ++i )
{
StockArrayArray[i] = new Stock[25];
}
// for freeing
for( int i = 0; i < n; ++i )
{
delete[] StockArrayArray[i];
}
delete[] StockArrayArray;
回答by Frederic Lachasse
The type of a variable to a dynamic array is a pointer to the first object of the array. You want an array of dynamically allocated Stock objects, so an array of pointers to Stock, so your variable is a pointer to a pointer to Stock:
动态数组的变量类型是指向数组第一个对象的指针。您需要一个动态分配的 Stock 对象数组,因此是一个指向 Stock 的指针数组,因此您的变量是一个指向 Stock 指针的指针:
int n = 4; // dynamic size of the array;
Stock** stockArray = new Stock*[n];
for (int i = 0; i != n; ++i)
{
stockArray[i] = new Stock();
}
and freeing it:
并释放它:
for (int i = 0; i != n; ++i)
{
delete stockArray[i];
}
delete[] stockArray;
回答by mono
Stock* stockArrayPointer = new Stock [4];
works only if the Stock class has a zero argument constructor if it does not have any zero argument constructor you cannot create an array of dynamic objects dynamically
仅当 Stock 类具有零参数构造函数时才有效,如果它没有任何零参数构造函数,则无法动态创建动态对象数组
you can as said create a array of dynamic object with a static array like
您可以如上所述创建一个动态对象数组,其中包含一个静态数组,例如
Stock stockArrayPointer[4]={Stock(args),Stock (args)};
but the syntax
但语法
Stock* stockArrayPointer=new Stock[4]{Stock(args),Stock (args)}; does not hold
or as said
use vectors...
vectors are memory allocated on heap
so the vector is a dynamic allocation
或者如上所述
使用向量...
向量是在堆上分配的内存,
因此向量是动态分配
vector<Stock> V;
V.push_back(Stock(args));
or
或者
V.push_back(new Stock(args));
The reason why
之所以
Stock* stockArrayPointer=new Stock[4]{Stock(args),Stock (args)};
does not hold is because this means you are using the new operator incorrectly
不成立是因为这意味着您错误地使用了 new 运算符
回答by Soumyadeep Thakur
I did something which worked perfectly:
我做了一些完美的事情:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class student {
string name;
int age;
int roll;
public:
student() {
name="";
age=0;
roll=0;
}
student (string n, int a, int r) {
name=n;
age=a;
roll=r;
}
void show_details ();
};
void student::show_details() {
cout << "Name: " << name << "\n";
cout << "Age: " << age << "\n";
cout << "Roll No: " << roll << "\n";
}
int main() {
string a; int b, c, n;
cin >> n;
student **obj;
obj=(student**)malloc(n*sizeof(student*));
for (int i=0; i<n; i++) {
cin >> a;
cin >> b;
cin >> c;
obj[i]=new student(a,b,c);
}
for (int i=0; i<n; i++) {
obj[i]->show_details();
}
for (int i=0; i<n; i++) free (obj[i]);
free (obj);
}
Yes... I used pointer to pointer for the array part, and it worked perfectly for variable sized arrays.
是的......我使用了指向数组部分的指针的指针,它非常适用于可变大小的数组。