1

I have a situation that I am attempting to write some c++ code to implement and I think I need to use templates but I am unsure. I don't think I understand templates well enough and just want to know if I am on the right track. I have never implemented a class that requires templates before.

I have a base class that needs to instantiate a new class and store it on a list for future access. The class that needs to be instantiated is derived from a base class that has one virtual method. After the class is instantiated, this method is called and the new instance is stored on a list. Another method is provided to get the instantiated object.

Say the virtual class is named foo:

class foo 
{
    public:
    virtual void process() = 0;
}

the user would create a class bar:

class bar : public foo
{
    public:
    void process() {};
}

The class that I think needs to be a template class:

class Filter<Foo* T>
{
public:
    // use value junk to derive foo
    // and put the new instance on a std::list
    add(Junk* junk);
    T get();

private:
    std::list<T> mFooList;
}

void Filter::add(Junk* junk)
{
    T* foo = new T();
    foo.p(junk);
    mFooList.push_back(foo);
}

T* Filter::get()
{
    if(!mFoolist.empty())
    {
        T* res = mFooList.back();
        mFooList.pop_back();
        return res;
    }
    return nullptr;
}
Nefarious
  • 458
  • 6
  • 21
  • You have several problems here. `class Filter` isn't the way to declare a template. And `void Filter::add(Junk* junk)` doesn't use `junk` in any way, so how is it supposed to affect the stored value? And also, `mFooList.pop_back();` is a `void` function so doesn't actually return anything. – Bo Persson Nov 26 '16 at 20:12
  • I just left out the detail for how junk is used as the coefficients for fir filter. I just thought it would add unneeded complexity to the question. – Nefarious Nov 26 '16 at 20:19
  • So let me understand your syntax clearer - you want `T` to be the type derived from `Foo` passed to the `Filter` class as a template parameter or am I over-interpreting your code? – W.F. Nov 26 '16 at 20:28
  • Like I said, I don't know I understand this. The user will define a class derived from foo, call it bar and tell it to the filter class. Then as new data is coming in, the filter will process the incoming data and create instances of the class bar. The user can get the filtered result via the get method. – Nefarious Nov 26 '16 at 20:34

1 Answers1

-1

You don't need to use a templatevin this situation. In c++ a derived class can be assigned to a pointer of the base class. You can just use foo pointers.

class foo
{
    public:
    virtual void process();
};

class bar1: public foo
{
     public:
     void process();
};

class bar2 : public foo
{         
    public:
     void process();
};

class filter
{
     private:
     std::list<foo*> _foos;

     public:
     foo* get();
     void add(foo* f); 
};

void filter::add(foo* f)
{
    _foos.push_back(f);
}

foo* filter::get()
{
    if(!_foos.empty())
    {
         return _foos.pop_back();
     }
    return nullptr;
}

You can then just add and get foos and bars

filter fil;
fil.add(new foo());
fill.add(new bar1());
fill.add(new bar2());

foo f = fill.get();
while(f != nullptr)
{
    f->process();
    delete f;
    f = fil.get();
}
Samuel
  • 1,073
  • 11
  • 22
  • When the user calls add, the argument is a different class that is know when the class filter is defined. It is the input class. I want to be able to allow the user to change the implementation of the filter by specifying a class that will process the input the way they want. – Nefarious Nov 26 '16 at 20:29
  • [`list::pop_back` is used incorrectly](http://en.cppreference.com/w/cpp/container/list/pop_back). Note the `void` return type. And careful with underscore prefixes. [Sometimes they mean more than you might think.](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Nov 26 '16 at 20:33