0

I am not a C++ guy, but I am trying to learn. I do not really know the lingo, so I am not sure what to search for, or how to ask this question. The best I can do is present the following example:

Define a generic class:

class IPlugin
{
public:
    virtual void go(const Image &src, Image &dst) = 0;
}

and then two 'plugins'

class pluginDenoise : public IPlugin
{
public:
    virtual void go(const Image &src, Image &dst)
    {
        denoise(src, dst);
    }
}

class pluginDeblur : public IPlugin
{
public:
    virtual void go(const Image &src, Image &dst)
    {
        deblur(src, dst);
    }
}

I now want to define a general framework, such as:

class Processor
{
public:
    Processor(IPlugin &unknown_plugin)
    {
        Image src = imRead("inpImage.jpg");
        Image dst;     

        unknown_plugin.go(src, dst);          

        imWrite(dst, "outImage.jpg");
    }
}

and from the main code, I want to call it something like

int main()
{
    pluginDeblur d;

    Processor p(d);
}

I understand this code will not compile... What is this even called? Is this possible?

matt
  • 170
  • 1
  • 14
  • 3
    This should compile perfectly fine as soon as you fix your `main` (the return type is `int`) and provide all the functions that are called (and preferrably make `dst` in the functions a reference to propagate the change to outside the function). – Xeo Jul 05 '12 at 21:40
  • Have you actually tried to compile it? Is it giving any errors? Is it not behaving as expected? Any particular reason for declaring the go methods from the 2 plugins as virtual? – Mihai Todor Jul 05 '12 at 21:41
  • Okay, I made the changes (return int for main; set dst to &dst); the question is updated. I have tried compiling something similar, perhaps I missed something... so are y'all saying this should work ok? – matt Jul 05 '12 at 21:48
  • Also, what is this called that I am trying to achieve? I'm assuming this is basic OO stuff, but not sure... – matt Jul 05 '12 at 21:49
  • 2
    This is basic *polymorphism*. – Xeo Jul 05 '12 at 21:51
  • For completeness, as Xeo pointed out, a simple Google search on 'polymorphism' reveals http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ which fully answers this question (with background). – matt Jul 05 '12 at 22:01
  • Class declarations end with a semicolon `class Foo { };` – qdii Jul 05 '12 at 22:33

0 Answers0