Does OpenCV get aways with const reference assignment?

I came across this piece of code in the openCV source ( cxoperations.hpp , line 1134, in the class definition Vector):

Vector(const Vector& d, const Range& r)
{
    if( r == Range::all() )
        r = Range(0, d.size());

    // some more stuff...
}

Note that the class Vectordoes not have a data element called r(and indeed, the identifier roccurs only in one place in the entire class definition, as a parameter in another method). Thus, it is obvious that there is a right to a link const.

I tried to reproduce a 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, does not compile: g ++ gives an error

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

The only way to make it work is to override it operator=as follows:

#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 and prints "0 3" as expected. The problem here is that

  • , , .
  • openCV operator=, Range (Range - Vector, 1033)

, - , openCV . , r = Range(0, d.size());, ?

+5
3

, cv Vector . , , . ,

Vector<int> a;
Vector<int> b(a, Range::all());

. OpenCV.

+7

, . - , .

:

template <class T>
class Vector
{
public:
   Vector()
   {
   }

   Vector(const Vector & x, const int & y)
   {
      y = 54;
   }
};
// template class Vector<float>;

void foo()
{
   Vector<float> v1;
//   Vector<float> v2(v1, 42);
}

GCC ( 4.5.1) , , .

+2

OpenCV Y! G dev-list "opencvlibrary-devel@lists.sourceforge.net".

0

All Articles