简体   繁体   English

我如何为 c++ 中分配的动态对象数组中的所有对象调用参数化构造函数?

[英]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::allocatorstd::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.

相关问题 使用参数化构造函数C ++动态分配对象数组 - Dynamical allocation of array of objects using parameterized constructor C++ 如何在C ++中创建参数化对象的数组? - How to create an array of parameterized objects in C++? 动态 memory 分配以使用带有 arguments c++ 的构造函数创建对象数组 - dynamic memory allocation to create array of objects using constructor with arguments c++ 如何使用 Visual Studio 2017 在 C++ 中为参数化对象数组使用唯一指针? - How can I use unique pointers for an array of parameterized objects in C++ using Visual Studio 2017? 如何将对象的动态数组传递给另一个类的参数化构造函数? - How do I pass a dynamic array of objects to a parameterized constructor of another class? 如何初始化 C 中的结构中的所有对象,就像构造函数初始化 C++ 中的所有 class 对象一样? - How can I initialize all objects from a struct in C the way constructor initializes all class objects in C++? 为什么我不能用c ++中的new调用参数化构造函数? - Why can't I call a parameterized constructor with new in c++? 如何使用参数化构造函数创建对象数组 - How to create an array of objects with parameterized constructor C ++管理对象的动态分配 - C++ managing the dynamic allocation of your objects C ++中对象的静态和动态内存分配 - static and dynamic memory allocation of objects in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM