[英]How can i call the parameterized constructor for all objects in my dynamic array of objects on allocation in c++?
When i define dynamic array of objects, i want to choose one parameterized constructor for all objects in my array.当我定义对象的动态数组时,我想为数组中的所有对象选择一个参数化构造函数。 without having to write for each object the chosen constructor like this
而不必像这样为每个 object 编写所选的构造函数
#include <iostream>
using namespace std;
class foo {
public:
foo ()
{
cout << "default constructor" << endl;
}
foo (int x)
{
cout << "parameterized constructor " << endl;
}
~foo ()
{
cout << "destructor" << endl;
}
};
int main (void)
{
int size = 3, parameter = 10;
foo *array;
array = new foo [size] { foo(parameter), foo(parameter), foo(parameter) };
cout << endl;
delete [] array;
return 0;
}
output output
parameterized constructor
parameterized constructor
parameterized constructor
destructor
destructor
destructor
So, as you can see from the above code, I can choose parameterized constructor for each object in my array array = new foo [size] { foo(parameter), foo(parameter), foo(parameter) };
因此,正如您从上面的代码中看到的,我可以为数组中的每个 object 选择参数化构造函数
array = new foo [size] { foo(parameter), foo(parameter), foo(parameter) };
. . However, if user inputs the
size
.但是,如果用户输入
size
。 same trick won't work同样的把戏不会奏效
When I searched for solution, I found that I can do it with copy constructor like this当我搜索解决方案时,我发现我可以用这样的复制构造函数来完成
#include <iostream>
using namespace std;
class foo {
public:
foo ()
{
cout << "default constructor" << endl;
}
foo (int x)
{
cout << "parameterized constructor " << endl;
}
~foo ()
{
cout << "destructor" << endl;
}
};
int main (void)
{
int size = 3, parameter = 10;
foo *array;
array = new foo [size];
cout << endl;
for (int i = 0; i < size; i++)
array[i] = foo(parameter);
cout << endl;
delete [] array;
return 0;
}
output output
default constructor
default constructor
default constructor
parameterized constructor
destructor
parameterized constructor
destructor
parameterized constructor
destructor
destructor
destructor
destructor
However, destructors are called for each object, and i don't want this i just want to do it while allocating for the first time但是,每个 object 都会调用析构函数,我不想这样,我只想在第一次分配时这样做
Thanks in advance, and I hope that there's a solution.在此先感谢,我希望有一个解决方案。
The simplest solution to this problem would be to use std::vector
which handles all those problems internally, eg:这个问题最简单的解决方案是使用
std::vector
在内部处理所有这些问题,例如:
#include <vector>
// skipping class declaration for brevity
int main (void)
{
int size = 3, parameter = 10;
std::vector<foo> array;
array.reserve(size);
cout << endl;
for (int i = 0; i < size; i++)
array.emplace_back(parameter);
cout << endl;
return 0;
}
However, if for some reason you want/need to do this by hand then you should be allocating a "raw buffer" and construct objects inside that buffer with placement new
- this will however also require you to manually call the destructors但是,如果出于某种原因您想要/需要手动执行此操作,那么您应该分配一个“原始缓冲区”并在该缓冲区内构造带有
new
放置的对象——然而,这也需要您手动调用析构函数
One possible example, doing everything "manually" could look like this一个可能的例子,“手动”做所有事情看起来像这样
int main (void)
{
int size = 3, parameter = 10;
foo *array = reinterpret_cast<foo*>(new char[size * sizeof(foo)]);
cout << endl;
for (int i = 0; i < size; i++)
new (&array[i]) foo(parameter);
cout << endl;
for (int i = 0; i < size; i++)
array[i].~foo();
delete[] reinterpret_cast<char*>(array);
return 0;
}
An arguably cleaner solution is to use std::allocator
and std::allocator_traits
- this would look like this一个可以说更干净的解决方案是使用
std::allocator
和std::allocator_traits
- 这看起来像这样
#include <memory>
// skipping class declaration
int main (void)
{
std::allocator<foo> alloc;
using alloc_t = std::allocator_traits<decltype(alloc)>;
int size = 3, parameter = 10;
foo *array;
array = alloc_t::allocate(alloc, size);
cout << endl;
for (int i = 0; i < size; i++)
alloc_t::construct(alloc, &array[i], parameter);
cout << endl;
for (int i = 0; i < size; i++)
alloc_t::destroy(alloc, &array[i]);
alloc_t::deallocate(alloc, array, size);
return 0;
}
I would use a for
loop:我会使用
for
循环:
array = new foo [size];
for(int i = 0; i < size; i++) {
array[i] = foo(parameter);
}
I don't see a simpler way to do it.我没有看到更简单的方法来做到这一点。 And with this method, your
size
can be parametrized easily.使用这种方法,您的
size
可以轻松参数化。
For your "destructor issue", use pointer on foo:对于您的“析构函数问题”,请在 foo 上使用指针:
array = new *foo[size];
for(int i = 0; i < size; i++) {
array[i] = new foo(parameter);
}
But do not forget to delete each foo
instance when needed:但不要忘记在需要时删除每个
foo
实例:
for(int i = 0; i < size i++) {
delete array[i];
}
delete[] array;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.