如何初始化数组指针对象

时间:2019-07-28 07:15:12

标签: c++ object pointers constructor

我在初始化(构造)对象的数组指针时遇到一些问题。请参阅下面的课程。类测试具有2个变量成员,一个将是数组的指针(值)及其大小(大小);以及带有参数的构造函数和析构函数。在main函数中,我将创建对象的数组指针,但对此有问题。如果我创建一个像这样的对象:

  • test obj(4);将创建一个对象,其实例的value数组为4。

然后,如果我想创建对象数组:

test *obj;
obj = new test[2]{4,7};

我将创建2个对象:obj[0]大4,obj[1]大7。

因此,如果我想创建更多对象:

test *obj;
obj=new test[100]{/*here I must write 100 numbers*/}

这就是问题所在。 因为我不能写这样的东西:

test *obj;
obj=new int[100]{4}

我希望每个value [](测试类的实例)大4,而我不会写100倍“ 4”。 我认为类比声明数组: 如果我写int array[5]={0,0,0,0,0},则必须写4次“ 0”,否则我也可以写: int array[5]={0},每个值都设置为0。(同样,如果写入int array[5]={5},则第一个索引将是5,其他索引将是0)。

我应该使用默认构造函数吗?我该怎么办?

#include <iostream>

using namespace std;

class test
{
private:
int* value;
int size;

public:
test(int size)
{
    this->size = size;
    value = new int[size];
}

~test()
{
    delete[]value;
}
};

2 个答案:

答案 0 :(得分:1)

您可以遍历指针以初始化每个元素

Tutorials - Programming.com
Tap into the collective intelligence of researchers who are working on the same problems you are - right now.

Array
(
    [0] => Array
        (
            [title] => 1,031
            [views] => HTML Cheat Sheet
        )

    [1] => Array
        (
            [title] => 390
            [views] => Best Java Training Institutes In Noida 
        )

    [2] => Array
        (
            [title] => 329
            [views] => Best Salesforce Training institutes in noida
        )

    [3] => Array
        (
            [title] => 382
            [views] => Top Quality Digital Marketing Training Institutes in Noida    
        )

    [4] => Array
        (
            [title] => 308
            [views] => Make your studies with professional Best Oracle Training Institutes in Noida    
        )

    [5] => Array
        (
            [title] => 374
            [views] => Create a Unique Project with a Best Linux Training Institutes in Noida
        )

    [6] => Array
        (
            [title] => 385
            [views] => Webtrackker Technology Best Dot Net Training Institutes Available To Guide the Students 
        )

    [7] => Array
        (
            [title] => 430
            [views] => Availability of My University Help Offers Great Benefit to Students
        )

    [8] => Array
        (
            [title] => 350
            [views] => Webtrackker Institute of Professional Studies: Hadoop Training Institute in Noida    
        )

    [9] => Array
        (
            [title] => 416
            [views] => The Best Quality Digital Marketing Training Institutes in Noida
        )

)

不过,最好使用test *obj = new test[100]; for(size_t i = 0; i != 100; ++i) { obj[i] = test(/*parameters*/); /* Remember to provide a move assignment operator which invalidates the pointer member, otherwise when the temporary variable is destroyed the new object pointer member will point to data no more available*/ } // ... delete [] obj;

std::vector

使用std::vector<test> obj(100, test(/*parameters*/)); std::vector对象初始化为传递其参数100次,使用指针分配(test)将默认构造每个元素,然后您将分配每个元素新值,这就是new test[100]是解决您问题的更好解决方案的原因

答案 1 :(得分:1)

您可以在堆栈上分配内存,而无需进行动态分配和内存管理。

test array[100];
std::fill(std::begin(array), std::end(array), test(100));

请注意,您将需要一个默认的构造函数。