class数组的C++动态分配
C++ dynamic allocation of class array
假设 class X
具有构造函数 X(int a, int b)
我创建了一个指向 X 的指针 X *ptr;
来为 class.
动态分配内存
现在创建 class X
的对象数组
ptr = new X[sizeOfArray];
到目前为止一切都很好。但是我想要做的是创建上面的对象数组应该调用构造函数 X(int a, int b)
。我尝试如下:
ptr = new X(1,2)[sizeOfArray];
不出所料它给了我编译时错误
error: expected ';' before '[' token|
如何创建一个对象数组来调用构造函数?
SizeOfArray
是用户在运行时输入的。
编辑:
正如天顶所回答的那样,我想要实现的目标是不可能的,否则会太复杂。那么我怎样才能使用 std::vector
呢?
这在当前的 C++ 标准中是不可能的,除非:
- 您为每个元素提供一个初始值设定项,或者
- 您使用
vector
.
参见:
- Object array initialization without default constructor
- How do I declare an array of objects whose class has no default constructor?
这似乎是 placement new...
的工作
这是一个基本示例:
#include <iostream>
#include <cstddef> // size_t
#include <new> // placement new
using std::cout;
using std::endl;
struct X
{
X(int a_, int b_) : a{a_}, b{b_} {}
int a;
int b;
};
int main()
{
const size_t element_size = sizeof(X);
const size_t element_count = 10;
// memory where new objects are going to be placed
char* memory = new char[element_count * element_size];
// next insertion index
size_t insertion_index = 0;
// construct a new X in the address (place + insertion_index)
void* place = memory + insertion_index;
X* x = new(place) X(1, 2);
// advance the insertion index
insertion_index += element_size;
// check out the new object
cout << "x(" << x->a << ", " << x->b << ")" << endl;
// explicit object destruction
x->~X();
// free the memory
delete[] memory;
}
编辑:如果我理解你的编辑,你想做这样的事情:
#include <vector>
// init a vector of `element_count x X(1, 2)`
std::vector<X> vec(element_count, X(1, 2));
// you can still get a raw pointer to the array as such
X* ptr1 = &vec[0];
X* ptr2 = vec.data(); // C++11
你不说 sizeOfArray
是变量还是常量。如果它是一个(小)常量,你可以在 C++11 中这样做:
X* ptr = new X[3] { {1,2}, {1,2}, {1,2} };
假设 class X
具有构造函数 X(int a, int b)
我创建了一个指向 X 的指针 X *ptr;
来为 class.
现在创建 class X
的对象数组 ptr = new X[sizeOfArray];
到目前为止一切都很好。但是我想要做的是创建上面的对象数组应该调用构造函数 X(int a, int b)
。我尝试如下:
ptr = new X(1,2)[sizeOfArray];
不出所料它给了我编译时错误
error: expected ';' before '[' token|
如何创建一个对象数组来调用构造函数?
SizeOfArray
是用户在运行时输入的。
编辑:
正如天顶所回答的那样,我想要实现的目标是不可能的,否则会太复杂。那么我怎样才能使用 std::vector
呢?
这在当前的 C++ 标准中是不可能的,除非:
- 您为每个元素提供一个初始值设定项,或者
- 您使用
vector
.
参见:
- Object array initialization without default constructor
- How do I declare an array of objects whose class has no default constructor?
这似乎是 placement new...
的工作这是一个基本示例:
#include <iostream>
#include <cstddef> // size_t
#include <new> // placement new
using std::cout;
using std::endl;
struct X
{
X(int a_, int b_) : a{a_}, b{b_} {}
int a;
int b;
};
int main()
{
const size_t element_size = sizeof(X);
const size_t element_count = 10;
// memory where new objects are going to be placed
char* memory = new char[element_count * element_size];
// next insertion index
size_t insertion_index = 0;
// construct a new X in the address (place + insertion_index)
void* place = memory + insertion_index;
X* x = new(place) X(1, 2);
// advance the insertion index
insertion_index += element_size;
// check out the new object
cout << "x(" << x->a << ", " << x->b << ")" << endl;
// explicit object destruction
x->~X();
// free the memory
delete[] memory;
}
编辑:如果我理解你的编辑,你想做这样的事情:
#include <vector>
// init a vector of `element_count x X(1, 2)`
std::vector<X> vec(element_count, X(1, 2));
// you can still get a raw pointer to the array as such
X* ptr1 = &vec[0];
X* ptr2 = vec.data(); // C++11
你不说 sizeOfArray
是变量还是常量。如果它是一个(小)常量,你可以在 C++11 中这样做:
X* ptr = new X[3] { {1,2}, {1,2}, {1,2} };