I need to store a bunch of classes in a container element of another class:
#include <list>
#include <cstdlib>
#include <cstdio>
class cont;
class elem {
public:
    virtual void do_something(int *buff) = 0;
};
class myelem1 : public elem {
public:
    virtual void do_something(int *buff) {
        buff[1] = 42;
    }
};
class myelem2 : public elem {
public:
    virtual void do_something(int *buff) {
        buff[2] = 33;
    }
};
class cont {
private:
    int *buff, size;
    std::list<elem> sub;
public:
    cont(int size) : buff((int*)malloc(size*sizeof(int))), size(size) {}
    ~cont() { if (buff) free(buff); }
    void add_worker(const elem &e) {sub.push_back(e);}
    void work() {
        for (elem & e : sub)
            e.do_something(buff);
    }
    void print() {
        for (int i=0; i<size; i++)
            printf("%03d ", buff[i]);
        printf("\n");
    }
};
int main() {
    cont c(10);
    c.add_worker(myelem1());
    c.add_worker(myelem2());
    c.work();
    c.print();
}
The above is a minimal example of what I need to do:
- I have a class contwith a dataset to be filled.
- I have a bunch of specific class elemdescendants each filling a specific part of dataset.
- I do not know at compile-time exactly which elements will be filled (mainwill actually read a config file and instantiateclass elemdescendants as needed).
- actual dataset won't be a simple array, of course.
- compilation of code as above bombs because /usr/include/c++/12/ext/aligned_buffer.h:54:25: error: cannot declare field ‘__gnu_cxx::__aligned_membuf<elem>::_Tp2::_M_t’ to be of abstract type ‘elem’
- changing line virtual void do_something(int *buff) = 0;tovirtual void do_something(int *buff) {};compiles without errors, but no data incont::buffis filled.
What am I missing?
