c++ - Can I provide readonly access to an OpenCV matrix? -


i have class combines number of opencv's cv::mat matrices.

is there way can provide both const accessors allowing clients read not write underlying data, , non-const accessors allowing clients read , write data.

i'm thinking of doing this:

class myclass {     cv::mat a;  public:     cv::mat a() { return a; }     const cv::mat& a() const { return a; } }; 

but protect underlying data being modified through const accessor? or protect cv::mat's header data?

if forced provide access cv::mat object itself, out of luck. data access through data pointer possible on const cv::mat. thus, code:

const cv::mat test = cv::mat::ones(3, 3, cv_8uc1); test.data[3] = 4; 

will compile , execute.

however, if need provide access data, provide wrapper functions cv::mat::begin() , cv::mat::end(), allow read-only access on const cv::mat:

class myclass {     cv::mat a;  public:     cv::matiterator_<uchar> begin() {return a.begin<uchar>();}     cv::matconstiterator_<uchar> begin() const {return a.begin<uchar>();}      cv::matiterator_<uchar> end() {return a.end<uchar>();}     cv::matconstiterator_<uchar> end() const {return a.end<uchar>();} };  myclass m;     const myclass& mref = m;     auto = mref.begin(); *it = 4;                //compile error here 

for example used uchar data type, it's easy enough make these pass along template parameter.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -