0

I have the following struct which I wanted to initialize

struct Box{
    int *dimval;
    int no;
    int dim;

    Box(dim){
        this->dim = dim;
        dimval = new int[dim]
    }

}

now in my main function. I wanted to initialize an array of Box struct, but this is problematic with my implementation.

int main(){
    Box *boxes;
    int num_box, dim;

    cin>>num_box>>dim;

    boxes = new Box[num_box](dim);// I know this is devil here. 
}

I want to have a dynamic array containing num_box Box items, each being initialized with a dynamic array of dim long. How can I do that?

8
  • You need a default constructor if you want to do that... Commented May 6, 2014 at 0:01
  • but with default constructor, I cannot pass dim parameter in it Commented May 6, 2014 at 0:02
  • 3
    Simply do not use raw arrays and use std::vector instead. Commented May 6, 2014 at 0:06
  • I am practicing writing c style code with c++, so stl data type is not preferred. how do you achieve this with a c code? Commented May 6, 2014 at 0:14
  • @Daniel I am practicing writing c style code with c++ Why? Why not just write straight C code instead of wasting time with C++, and in the process, change the tag to C instead of C++. Commented May 6, 2014 at 0:18

1 Answer 1

4

You cannot create an array of a type unless it has a default constructor, and then you can't initialize each of them. However, you can initialize a vector with a default object, which is pretty much what you're asking for here.

#include <vector>

int num_box, dim;
cin >> num_box >> dim;
vector<Box> boxes(num_box, Box(dim));

Note that you need a copy constructor to handle the copying of dimval...

#include <algorithm>

struct Box
{
    // ...

    Box(const Box& that)
    {
        this->dim = that.dim;
        this->no = that.no;
        this->dimval = new int[dim];
        copy(that.dimval, that.dimval + that.dim, this->dimval);
    }
};

... but you can use the default copy constructor if you simply replace your dimval pointer with a vector, too, since vectors handle copy (that would fix a leak, too).

struct Box
{
    vector<int> dimval;
    int no;
    int dim;

    Box(int dim)
    : dimval(dim)
    {
        this->dim = dim;
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

any way to do it under the context of dynamic allocated pointer? I am maintaining my version of dynamic array instead of using stl vector
You cannot invoke a constructor other than the default constructor when you create an array with new, and there needs to be a default constructor to be able to create an array with new. Your only other solution is to use a placement new, but this really opens a can of worms.
Anyways, here's how you'd do it, but I personally believe that you would shoot yourself in the foot if this is for anything more than learning purposes: stackoverflow.com/questions/222557/…
@Daniel Why you use your own version of std::vector if you can freely choose between the two ?
@Daniel I am maintaining my version of dynamic array instead of using stl vector and while you're doing that, some other C++ programmer is using std::vector and has made more progress in finishing their program.
|

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.