I have a C++ class that contains a private C array as follows,
class DataObject {
private:
double* data_array_;
};
Due to restrictions from other parts in the program that are not shown, I can't use a std::vector<double>, can't use the DataObject constructor to initialize the array (initializer list?), and would prefer not to allocate the array on the heap. As such, I have to initialize the array in an init() function, which takes in the number of array elements as its argument. So far I've tried the following, but unfortunately they don't seem to work:
Attempt 1:
void DataObject::init(unsigned int num_elements) {
data_array_[num_elements];
}
Attempt 2:
void DataObject::init(unsigned int num_elements) {
data_array_ = double[num_elements];
}
Thus, I was wondering if there's another way to initialize a stack allocated private C array given the above restrictions.
init()is called. And don’t forget to follow the Rule of 3/5/0 to manage the array correctly.vector<double>? It should be pretty easy to plug-in whereever adouble*is needed (just usedata_array_.data()in those places). Why don't you want to use the heap?