I am programming a simulation for my bachelor's thesis and I'm not an expert in C++, and after having searched for quite some time now without finding a convenient answer, or question, for that matter, I resort to you guys.
My problem is as follows. I have some class that has a couple of member fields num_a
and num_b
besides others that can be stored on the stack. Now both these values are roughly of size 1000-2000. What I need now is another class member for SampleClass
, namely a 2-dimensional boolean array of size num_a
* num_b
. Due to its size, it needs to be alloacted on the heap. It needs to be contiguous in memory, so storing a pointer to pointers to arrays does not work for me.
SampleClass : Object {
public:
uint16_t num_a;
uint16_t num_b;
??? // some data structure for my 2d array
// simple constructor
SampleClass(num_a, num_b);
}
I declare my classes in a header file .h
and implement the functions and the constructor in a source file .cc
.
As you see, the values of both num_a
and num_b
are not pre-determined and thus not const
. My question is, how do I (in a simple way) declare this thing in the header file, and how do I initialize it in the constructor in the source file?
A thing that I have found that uses vector is the following:
// header file
std::vector<std::vector<bool>> *coverage_matrix;
// source file
coverage_matrix = new std::vector<std::vector<bool>>();
coverage_matrix->push_back(something); // do something with it
Does this last approach work and more importantly, is it as efficient as a solution that does not rely on std::vector
would be?
Thanks for your answers.