C
captaincurk
Hello there,
I have the following problem:
// abstract base class
class A {
public:
virtual void addItems() =0;
};
class B : public A {
public:
virtual void addItems() {
// here I need to instantiate objects of a given type
}
}
In B's something() function, I need to fill an internal list with
objects of a specific type. I need some way to pass my classname to
the function. Naturally I would try
template <class InstantiationClass> virtual void addItems() {
list.push_back(new InstantiationClass());
}
Unfortunately that does not work, because pure virtual template
functions obviously don't work. Now I could move the template up to
the class B, but that is not very useful, as in my main program I will
call addItems very often and with different classes as parameters. Is
there an easy way to do this?
One way I could think of is to make a global constructor for each
class I want to use and then pass a function reference, but that
creates a LOT of new functions, which are only called once. And also
since my code resides in a framework, the call to addItems should be
very easy and straightforward and work with custom classes.
Thanks in advance and cheers,
Jonas
I have the following problem:
// abstract base class
class A {
public:
virtual void addItems() =0;
};
class B : public A {
public:
virtual void addItems() {
// here I need to instantiate objects of a given type
}
}
In B's something() function, I need to fill an internal list with
objects of a specific type. I need some way to pass my classname to
the function. Naturally I would try
template <class InstantiationClass> virtual void addItems() {
list.push_back(new InstantiationClass());
}
Unfortunately that does not work, because pure virtual template
functions obviously don't work. Now I could move the template up to
the class B, but that is not very useful, as in my main program I will
call addItems very often and with different classes as parameters. Is
there an easy way to do this?
One way I could think of is to make a global constructor for each
class I want to use and then pass a function reference, but that
creates a LOT of new functions, which are only called once. And also
since my code resides in a framework, the call to addItems should be
very easy and straightforward and work with custom classes.
Thanks in advance and cheers,
Jonas