C++ Allocate Vector Size in class variable without

2019-02-25 09:50发布

问题:

So I have a class (HostObject) that has a vector of complex classes (object1) inside it. Such as the pseudocode depicted below

#DEFINE ARRAY_SIZE 10
class HostObject {
    //other member variables
    vector<Object1> vec(ARRAY_SIZE);

    //default constructor
    HostObject(){
        //leave vector default constructed
    }

    //my custom constructor
    HostObject(double parmeter1, double parameter2, doubleparameter3) {

        //some code doing some calculations 

        for (int i = 0; i <ARRAY_SIZE; i++){

            vec.push_back(Object1( double p1, int p2, double p3, intp4));

        }
    }
}

I know the way this code is written any time HostObject is created the vector will be initialized with default constructed Object1s. My code requires a vector so I would like the compiler to know what the size of the vector is so it can appropriately allocate the memory needed for my vector. I know I could use reserve if I wanted a more dynamic allocation.

I guess my question is: Is their a way to reserve space for a vector when defining it that does not require default initialization of the objects in it or use of the reserve function?

Edit:

My goal is to have the memory space allocated so when I construct an array of objects of HostObject type the get allocated the correct amount of memory. Does the memory size of an object get determined based on the result of the default constructor?

回答1:

There is no constructor for std::vector for for reserving capacity. The only way you can reserve capacity is to use reserve member function.

#DEFINE ARRAY_SIZE 10
class HostObject {
    //other member variables
    vector<Object1> vec;

    //default constructor
    HostObject(){
        vec.reserve(ARRAY_SIZE);
    }

    //my custom constructor
    HostObject(double parmeter1, double parameter2, doubleparameter3) {
        vec.reserve(ARRAY_SIZE);
        //some code doing some calculations 

        for (int i = 0; i <ARRAY_SIZE; i++){

            vec.push_back(Object1( double p1, int p2, double p3, intp4));

        }
    }
}

Read more about std::vector



回答2:

You could do this by calling reserve() in default constructor.

#DEFINE ARRAY_SIZE 10
class HostObject {
  //other member variables
  vector<Object1> vec; //vec(ARRAY_SIZE);  // not here

  //default constructor
  HostObject() {
    //leave vector default constructed
    vec.reserve(ARRAY_SIZE);               // but here
  }

  //my custom constructor                  // no need to change, since push_back will increase size automatically
  HostObject(double parmeter1, double parameter2, doubleparameter3) {

    //some code doing some calculations 

    for (int i = 0; i <ARRAY_SIZE; i++) {

      vec.push_back(Object1(double p1, int p2, double p3, intp4));

    }
  }
}

Also consider using static unsigned array_size = 10; as a member of class HostObject, so that you can change the size dynamically every time creating an object of HostObject.

HostObject ho1;  // array size is 10
HostObject::array_size = 20;
HostObject ho2;  // array size is 20