From: Fabian on
Hallo,

I have two image processing class methods which - to the outside - basically
do the same operation on a vector of images. The difference is that one
function produces new images, allocating the necessary memory; the other
function directly modifies the source images' data. Internally two different
functions have to be called.
I would like to get rid of the necessary overload (then I can implement the
command pattern).
For this I would have to be able to check whether the passed output vector
is the same as the input one (see code snippet below) without comparing the
vector contents via their "==" operator. Is this somehow possible?

Thanks in advance,

Fabian

bool StackOpLRDeconvXY::Execute(const vector<Spimage*>& inPlanes, const
unsigned int planeNum, vector<Spimage*>* outPlanes) const
{
try
{
if (inPlanes != *outPlanes) // This will compare the vector elements, right?
{
// take inplanes as source and outPlanes as target
}
else
{
// take inPlanes as source and target
}
}
catch(...)
{
PrintError("StackOpLRDeconvXY::Execute()", "error calculating plane");
return false;
}
return true;
}

From: Ulrich Eckhardt on
Fabian wrote:
> For this I would have to be able to check whether the passed output vector
> is the same as the input one (see code snippet below) without comparing
> the vector contents via their "==" operator.

Note: the ==operator used on vectors (or any other type) only returns if
they are equivalent (i.e. it's always about the value) while the thing you
seem to want it so compare for identity (i.e. whether they are the same
object).

For identity comparisons, there is a simple way in C++, and that is
comparing the addresses.

> bool StackOpLRDeconvXY::Execute(const vector<Spimage*>& inPlanes, const
> unsigned int planeNum, vector<Spimage*>* outPlanes) const
> {

Note: I'm not sure what it means if outPlanes is null. If this is the vector
receiving the data and that vector must be provided, you could use a
reference instead to make that clear.

> if (inPlanes != *outPlanes) // This will compare the vector
> // elements, right? {

Yes. Here, you could instead use

if( &inPlanes != outPlanes)

to check if the two vectors are the same.

> catch(...)
> {
> PrintError("StackOpLRDeconvXY::Execute()", "error calculating plane");
> return false;
> }

Not that it matters to your initial problem, but why swallow any exceptions
here? The calling code probably can't continue anyway and is only cluttered
with checks. Just a suggestion though.

cheers

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932