Hello,
I am developing a library in C++ where users/programmer will extend a class BaseClass
that has a method initArray
. This method should be implemented by the user/programmer and it should normally initialize all elements of the array m_arr
.
Here is a snipplet, modified to this example:
class BaseClass {
public:
BaseClass(int n) {
m_arr = new double[n];
size = n;
};
virtual ~BaseClass();
int size;
double* m_arr;
virtual int initArray();
};
Sometimes, the user/programmer implements a initArray
that does not initialize some elements of m_arr. What I would like is to create a function in my library that checks if initArray
did initialize all elements of m_arr
. This function should be called by a sanity-check rutine at runtime.
My question: is it possible to detect changes on this array? I can only think of initializing the array with some invalid values (like NaN or Inf), call initArray
and check that all values have changed.
Thanks for your ideas,
David
Edit
Here is an example of the client code that I am trying to detect:
// .h:
class MyExample : public BaseClass {
public:
MyExample();
virtual ~MyExample();
virtual int initArray();
};
// .cpp:
MyExample::MyExample() : BaseClass(3) { }
MyExample::~MyExample() { }
int MyExample::initArray() {
m_arr[0] = 10;
//m_arr[1] = 11; // let's say someone forgot this line
m_arr[2] = 12;
return 0;
}
So, by forgetting the m_arr[1]
this element is not initialized and could cause problems in future calculations. That's what I would like to check.