Detect variable change at run time in C / C ++

I am developing a C ++ library where users / programmers will extend a class BaseClassthat 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 the snipplet modified in 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 a user / programmer implements initArraythat does not initialize some m_arr elements. I would like to create a function in my library that checks to see if initArrayall the elements have executed m_arr. This function must be called by the health check service at runtime .

: ? (, NaN Inf), initArray , .

,


Edit

, :

// .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 say someone forgot this line
     m_arr[2] = 12;
     return 0;
}

, m_arr[1], . , .

+5
10

++. , , - Ruby on Rails, .

[] .

:

1) [] .

2) [] , , . , .

, .

+3

std::vector? push_back, , , .

+5

"" , , initArray() ... ? initArray() . m_arr private:

class BaseClass {
private:
    size_t size;
    auto_ptr< vector< double > > m_arr;

public:
    BaseClass(size_t n) {
        size = n;
     };
     virtual ~BaseClass();

protected:
     virtual auto_ptr< vector< double > > initArray() const;
};

(: initArray , , , . , initArray, .)

+3

:

, InitArrayImpl, .

initArray , , InitArrayImpl, , , , .

initArray.

+2

inf NaN . .

, - . , InitArray() .

+1

,

  • m_arr,
  • bools , m_arr m_field_initialized = new bool[n], .
  • ( ) accessor, m_arr m_field_initialized
  • setVal(int i, double val), m_arr[i] m_field_initialized[i] true;
  • , m_field_initialized true.

, , . , .

double * directAccess() {
   if (m_arr_initialized) return m_arr;
   else return 0;
}

m_arr_initialized .


, , m_arr , , m_arr . , . m_arr allocate(std::size_t size) allocate(std::size_t size, double initVal) , allocate(std::size_t size, double (*callback)(std::size_t element)), . .


edit: ( ) BaseClass. . :

class InitializerInterface
{
   public:
     virtual double get(int element) const = 0; 
}

 BaseClass(int n, const InitializerInterface & initializer) {
     m_arr = new double[n]; 
     size = n;
     for (int i = 0; i < n; i++)
        m_arr[i] = initializer.get(i);
 };

. , , get() . .

// , const-correct

+1

++ C , . , C-struct ++-, , .

-, , std:: generate. . , , , , . , , , .

- , NaN inf , , , . , , 0 , , , .

, , - () .

+1

, : , Neil push_back()?

, std::vector<> ( ?) boost::optional<double>. ( boost library?)

, ? , BaseClass, initArray(). ?

0

, . , . , , , . NaN , , -1 MAX_DOUBLE. , ctor, .

1000 , , .

0

. .

0
source

All Articles