Is there a way to store methods in some type of vector in c++?

Viewed 231

This is my first time here.

I'm currently trying to write a class for post-processing images by going through all pixels and doing stuff to the colours there.

My idea is to add methods like posterize() or rgbSplit() etc. When they are initialized it won't go through all pixels for each effect because that's not very performant. I want it to kind of store the fact that it should process these effects in the given chain later when I use a method called process().

But therefore I'd have to kinda store the reference to the effects' code in a list (like a vector), right? Because then I could go into the for-loop of the x and y-coordinates and add another for-loop there that goes through all methods that should be applied to the pixels.

The problem is, I have no idea how to store a method in a vector. Is that even possible? or are vectors only for objects? Or can I somehow "objectify" a method? Or what would be the thing that I have to do now?

4 Answers

This question is very broad. But I understand that you are at the beginning, and need to know if it's possible, before writing some code.

So here a quick low-level answer:

  • yes you can have vectors of function pointers;
  • yes you can have vectors of std::function, which is much more powerful and flexible than raw function pointers;
  • yes you can "objectify" a method by putting it as virtual function in a class and using it as if it were a function (either using a method name and implementing a command pattern, or using operator() to implement a classical functor).
  • yes you can even chain in a more flexible way your different raster operations, by replacing the array with a variant of the chain of responsibility pattern

But at a higher level, you need to better think about your design:

  • some image filters can work at a pixel level, and then your approach is fine
  • but some image filter work on a group of adjacent pixels. If you would work pixel by pixel, the chaining would not work properly, since there will be alredy transformed pixels and still original untransformed pixels in the groups that are processed.

So your design needs to cope with both kind of algorithms. For example you could first apply the preliminary pixel transformation in the chain to all the pixels, then only run the algorithm on the groups of already transformed pixels, then continue in the chain.

Moreover, as Daniel McLaury pointed out in the comments, an additional question is whether it makes sense from a performance point of view to call an algorithm pixel by pixel. Maybe chaining algorithms is a good idea, but at the image transformation level and not the pixel level.

Once you have thought about this, and started implementing a solution with one of the above mentioned ideas, you may come back here with more precise questions, illustrated with some code.

i'm currently following the tutorial of the cherno on function pointers because i thought maybe i need someone who goes through it in detail and extremely well structured. https://www.youtube.com/watch?v=p4sDgQ-jao4&t=306s

at 5min he made a function pointer by saying void(*cherno)() = HelloWorld;

i tried to do that as well but get an error. here's my code:

class ImagePP {
public:
    ImagePP() { }

    void someFunc() { DBG("someFunc started"); }

    void process() {
        DBG("process Started.");

        void(*function)() = someFunc;
        function;
    }
private:

};

it underlines someFunc and the error says:

a value of type "void (ImagePP::)()" can not be used to initialize an entity of type "void()()"


if i understand it right it says i can't do that because i'm not doing it in main, but in a class. but what can i do about it then? i want to use it in this class

class ImagePP {
public:
    std::function<void> someFunc(int i) { DBG("someFunc started: " << i); }

    void process() {
        DBG("process Started.");

        typedef std::function<void>(ImagePP::*FuncPtr)(int);
        FuncPtr function = someFunc;
        function(5);
    }
};

this is what i got now. realized it wants the class name there, but now it underlines (5) even though the function wants an integer. it says:

expression preceding parentheses of apparent call must have (pointer-to-) function type

i have the same error when i use normal void instead of std::function

Related