c++ - OpenCV gets aways with assigning to a const reference? -


i stumbled upon piece of code in opencv source ( cxoperations.hpp, line 1134, in definition of vector class ):

vector(const vector& d, const range& r) {     if( r == range::all() )         r = range(0, d.size());      // more stuff... } 

note vector class has no data member called r (and indeed, identifier r occurs in 1 more place in entire class definition, parameter in method). apparently, right there assignment const reference.

i tried reproduce minimal example:

#include <iostream>  class foo {   public:     int _a;     foo(int a) : _a(a) {} };  int main() {     foo x(0);     const foo& y = x;     printf("%d\n", y._a);     y = foo(3);     printf("%d\n", y._a); } 

this, of course, fails compile: g++ gives error

test.cpp:15: error: passing `const foo' `this' argument of `foo& foo::operator=(const foo&)' discards qualifiers 

the way got work overriding operator= this:

#include <iostream>  class foo {   public:     int _a;     foo(int a) : _a(a) {}     foo& operator=(foo rhs) const     {         foo& tmp = const_cast<foo&>(*this);         tmp._a = rhs._a;         return const_cast<foo&>(*this);     } };  int main() {     foo x(0);     const foo& y = x;     printf("%d\n", y._a);     y = foo(3);     printf("%d\n", y._a); } 

this compiles, , prints "0 3" expected. problem here that

  1. anyone writes code should have hands cut off
  2. in opencv source above, there no redefinition of operator= takes range parameters (range-related functions above definition of vector, starting @ line 1033)

obviously i'm missing something, since opencv source compiles. question is, going in in r = range(0, d.size()); line makes legal?

i notice cv's vector class template. if class method never instantiated, there no compiler error. guess if try

vector<int> a; vector<int> b(a, range::all()); 

you'll compiler error on suspicious-looking line. can report bug in opencv source.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -