I'm trying to define template class inside of nontemplate class, below you may see the code of what i'm actually trying to do (it doesn't compilable for obvious reason). The main question is how can I realize that using C++11 (preferable) or C++14?
-
Actually I have solution using std::variant or same function in boost library, but I need to know another way to solve that.
-
I found old similar question, and answer by Anne Quinn sounds valuable (declare subclasses for each type I need) but how to apply that in code right?
Code:
#include <vector>
#include <cstdint>
enum Type {
INT16,
UINT32
};
template<typename T>
class Buffer {
public:
Buffer(uint32_t paramSize) {
buffer.resize(paramSize);
}
private:
std::vector<T> buffer;
};
class Foo {
public:
Foo(Type paramType, uint32_t paramSize) {
switch(paramType) {
case UINT32:
buffer = Buffer<uint32_t>(paramSize);
break;
case INT16:
buffer = Buffer<int16_t>(paramSize);
}
}
private:
Buffer buffer;
};
int main() {
Foo var(INT16, 30);
return 0;
}
Thanks!
Aucun commentaire:
Enregistrer un commentaire