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());
}
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());, ?