c++ - vector/array bounds check only when a define is declared -


i've create own container inherited vector. reimplement operator[] in way makes checking bounds decided #define.

so putting example, , ignoring template parmameters they're complicated , irrelevant

class myarray : vector<double> {     //...     virtual double& operator[](const size_type& index);     virtual const double& operator[](const size_type& index) const;     //... }   double& myarray::operator[](const size_type& index) {     #ifdef debug_enabled         return this->at(index);     #else         return (*this)[index];     #endif } 

however, doesn't work because since operator[] overloaded, calling operator[] @ #else become recursive.

i make check bounds based on #define, , not based on whether use std::vector<>::at() or std::vector<>::operator[].

how can resolve this?

edit: since it's offered lot use std::vector member instead of inheriting, have mention doing isn't solution me because i'll have reimplement member functions of std::vector. that's not pleasant do!

simply call base class member functions required:

double& operator[](size_type index) {     #ifdef debug_enabled         return std::vector<double>::at(index);     #else         return std::vector<double>::operator[](index);     #endif } 

you should provide const version of too. note don't want make operator virtual.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -