call your own function call operator
Jul 13, 2012 at 9:26pm UTC
Hello
I have a class that maps an image into a std::vector.
Access is provided by overloading the function call operator:
1 2 3
dataType& operator ()(int const x, int const y) {
return data[y*sizeX + x];
}
Now I was wondering if I can call this from member functions of this class.
I tried the following:
1 2 3
(i,j)
*this (i,j)
this ->(i,j)
The first one compiles but produces a wierd result I don't understand. The compiler complains about the other two.
I am on Ubuntu i.e. using g++.
Any help is greatly appreciated.
Jul 13, 2012 at 9:35pm UTC
1 2 3
(*this )(i,j)
//or alternatively
operator ()(i,j)
Jul 13, 2012 at 10:08pm UTC
Thanks for your help. They both work however it seems that (i,j) was right all along as the result is the same.
The problem seems to be something else. Maybe somebody can help...
I have the following class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
template <class dataType>
class Image2D {
public :
Image2D(): data(0), sizeX(0), sizeY(0) {}
Image2D(int const sizeX, int const sizeY): data(sizeX*sizeY, dataType(0)), sizeX(sizeX), sizeY(sizeY) {}
int getsizeX() const { return sizeX; }
int getsizeY() const { return sizeY; }
dataType& operator ()(int const x, int const y) {
return data[y*sizeX + x];
}
// debugging
void printImage() {
cout << endl;
for (int j = 0; j < sizeY; ++j) {
for (int i = 0; i < sizeX; ++i) {
cout << (i,j) << " " ;
}
cout << endl;
}
}
protected :
int sizeX, sizeY;
std::vector<dataType> data;
};
And this in main file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
void pattern(Image2D<double >& image) {
for (int i = 0; i < image.getsizeX(); ++i) {
image(i,0) = 1;
image(i,image.getsizeY()-1) = 2;
}
for (int j = 0; j < image.getsizeY(); ++j) {
image(0,j) = 3;
image(image.getsizeX()-1,j) = 4;
}
}
void printImage(Image2D<double >& image) {
cout << endl;
for (int j = 0; j < image.getsizeY(); ++j) {
for (int i = 0; i < image.getsizeX(); ++i) {
cout << image(i,j) << " " ;
}
cout << endl;
}
}
int main() {
Image2D<double > test(10, 10);
pattern(test);
test.printImage();
printImage(test);
return 0;
}
When I run this I get the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
3 1 1 1 1 1 1 1 1 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 0 0 0 0 0 0 0 0 4
3 2 2 2 2 2 2 2 2 4
Any idea why the output of the two calls isn't the same? The second output is the one I'm looking for in the first one aswell.
ps I edited out assertions in the class as they shouldn't matter for my problem here.
Jul 13, 2012 at 10:18pm UTC
Jul 13, 2012 at 10:30pm UTC
Thanks again, it works now using
I'm editing the code over ssh on a server. For some reason I had to manually delete all the .o files for the changes to kick in. Well at least I got to learn about the comma operator.
Topic archived. No new replies allowed.