|
From: Fabian on 5 May 2008 06:59 Hello, I have a struct defined as follows: class ImageMetrics { public: ImageMetrics(): Width(0), Height(0), BitsPerSample(0), SamplesPerPixel(0), BytesPerPixel(0){} int Width; int Height; int BitsPerSample; int SamplesPerPixel; int BytesPerPixel; }; which is defined __dllexport. I have to compare whether two instances have the same values (and I wanto to avoid comparing the members). Just using "==" obviously doesn't work. So: - (How) can I implement a "==" operator for a struct? Just a bool operator == (ImageMetrics& CmpMetrics); produces error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Spimaging::ImageMetrics' (or there is no acceptable conversion) - Should I make it a class? - Is there another quick way? Thanks for your help, Fabian
From: Fabian on 5 May 2008 07:05 Appendix: > Just a > bool operator == (ImageMetrics& CmpMetrics); Of course I would add a corresponding implementation like: bool ImageMetrics::operator==( ImageMetrics& i_orCmpMetrics ) { if ( (BitsPerSample == i_orCmpMetrics.BitsPerSample) && (BytesPerPixel == i_orCmpMetrics.BytesPerPixel) && (Height == i_orCmpMetrics.Height) && (SamplesPerPixel == i_orCmpMetrics.SamplesPerPixel) && (Width == i_orCmpMetrics.Width) ) return true; else return false; }
From: Alex Blekhman on 5 May 2008 07:23 "Fabian" wrote: > I have a struct defined as follows: > > class ImageMetrics > { > public: > ImageMetrics(): Width(0), Height(0), BitsPerSample(0), > SamplesPerPixel(0), BytesPerPixel(0){} > int Width; > int Height; > int BitsPerSample; > int SamplesPerPixel; > int BytesPerPixel; > }; > > which is defined __dllexport. I have to compare whether two > instances have > the same values (and I wanto to avoid comparing the members). > Just using "==" > obviously doesn't work. If your struct consists only of plain integers, then you can compare it with `memcmp' function: ImageMetrics im1 = ... ImageMetrics im2 = ... if(memcmp(&im1, &im2, sizeof(ImageMetrics)) == 0) { // identical } You can use `memcmp' withing `operator ==' as well (pay attention to correct constness): bool ImageMetrics::operator==( const ImageMetrics& i_orCmpMetrics) const { return ( memcmp(&orCmpMetrics, this, sizeof(ImageMetrics)) == 0 ); } HTH Alex
From: Fabian on 5 May 2008 08:22 Hi Alex, "Alex Blekhman" wrote: > You can use `memcmp' withing `operator ==' as well (pay attention > to correct constness): Thanks a lot for the suggestion. But with this I still have the C2678. Any ideas? Thx, Fabian
From: Alex Blekhman on 5 May 2008 08:38 "Fabian" wrote: > Thanks a lot for the suggestion. But with this I still have the > C2678. Any ideas? Check again that you have the decrataion and the definition of the `operator ==' are the same. The following code compies and runs for me: class ImageMetrics { public: ImageMetrics(): Width(0), Height(0), BitsPerSample(0), SamplesPerPixel(0), BytesPerPixel(0){} bool operator ==(const ImageMetrics& other) const; int Width; int Height; int BitsPerSample; int SamplesPerPixel; int BytesPerPixel; }; bool ImageMetrics::operator ==(const ImageMetrics& other) const { return (memcmp(&other, this, sizeof(ImageMetrics)) == 0); } int main() { ImageMetrics im1; ImageMetrics im2; if(im1 == im2) { // equal } return 0; } Alex
|
Next
|
Last
Pages: 1 2 Prev: How can I cotrol the position of the cursor in Word? Next: Incremental linker VS2008 |