0

I have a little problem to initialize (constructor) an array pointer of object. See the class below. Class test has 2 variable member, a pointer (value) that will be an array, and his size (size); and a constructor with parameters, and a destructor. In main function, I will create an array pointer of objects, and I have problem with it. If I create a single object like:

  • test obj(4); it will create a object, and his instance, value array is big 4.

Then if i want to create an array of objects:

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

I will create 2 object: obj[0] that is big 4, and obj[1] that is big 7.

So if I want to create more object:

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

and this is the problem. Because I cant write something like this:

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

I want that each value[] (instance of test class) is big 4, and I wont write 100 times "4". I thought the analogy of declaring array: If I write int array[5]={0,0,0,0,0}, I must write 4 times "0", or I can write also: int array[5]={0} and each value is set to 0. (it's also true that if write int array[5]={5}, first index will be 5 and others 0).

Should I use a default constructor? What should I do?

#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;
}
};
3
  • 1
    Why not just use a std::vector? Commented Jul 28, 2019 at 7:17
  • Yes it's another possible way. But i would know how to do (just for knowledge). Commented Jul 28, 2019 at 7:21
  • 1
    test doesn't follow rule of 5/3/0. leading to issue with copy/move. using std::vector for value would solve that issue. Commented Jul 28, 2019 at 7:46

2 Answers 2

2

You can allocate the memory on the stack and get rid of dynamic allocation and memory management.

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

Note that you would need a default constructor here.

Sign up to request clarification or add additional context in comments.

Comments

1

You can iterate over your pointer to initialize each element

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;

However it would be better to use std::vector

std::vector<test> obj(100, test(/*parameters*/));

Using std::vector your test object is initialized 100 times passing its arguments, using a pointer the allocation (new test[100]) will default construct every element, then you are going to assign each element the new value, that's why std::vector is a better solution to your problem

9 Comments

Sorry, i accidentally wrote new int[100], I've edited the answer
prog.cpp:25:25: error: no matching function for call to ‘test::test()’, and if you add a default constructor, it will crash at runtime.
For the pointer alternative a default constructor is nedded. About the crash, can you post the error you are receinving?
It's a double-free.
Yes, you need a default constructor. test::test(){ }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.