List/Vector of Template-Class objects

Hi folks,

I have a problem with a vector containing objects from a template class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
template <class T> class TemplClass{
public:
 T value;
 virtual void smthSmth(void) = 0;
};

template <class T> class SubTemplClass1: public TemplClass<T>{
public:
 virtual void smthSmth(void){ cout<<"SubClass1"<<endl; }
};

template <class T> class SubTemplClass2: public TemplClass<T>{
public:
 virtual void smthSmth(void){ cout<<"SubClass2"<<endl; }
};

class Container{
private:
 vector<TemplClass> list;
public:
 void add(TemplClass tcObject){ list.push_back(tcObject); }
 void smthSmthLast(void){
  list.back().smthSmth();
  list.pop_back();
 }
};


The thing is, the compiler means i need to add a template parameter for TemplClass!

Then i tried smth like this:

1
2
3
4
5
6
7
8
9
10
class Container{
private:
 vector<TemplClass<>> list;
public:
 void add(TemplClass<> tcObject){ list.push_back(tcObject); }
 void smthSmthLast(void){
  list.back().smthSmth();
  list.pop_back();
 }
};


But it still wont work!

The solution for me was to make a non template base class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class BaseClass{
public:
 virtual void smthSmth(void) = 0;
};

template <class T> class TemplClass: public BaseClass{
public:
 T value;
 virtual void smthSmth(void) = 0;
};

//And take that as vector element type
class Container{
private:
 vector<BaseClass> list;
public:
 void add(BaseClass tcObject){ list.push_back(tcObject); }
 void smthSmthLast(void){
  list.back().smthSmth();
  list.pop_back();
 }
};


This works fine in case of NOTHING will be added or changed on any inheritting class!

But that is not a proper solution in the view of object oriented programming.
Because the base class has to have the sum of all methods (of all derived classes) which will be called by the container. That does not make any sense! That's not dynamic or generic at all!!!

Is that really an unsolveable problem??? Or is there smth like TmplClass<?> in Java.

(Hint: The code lines are out of my head, i didn't test it. So it could be possible that syntax errors are inside.)
The thing is, the compiler means i need to add a template parameter for TemplClass!
Of course, after all TemplClass is a template. ¿what are you trying to do?


By the way, vector<BaseClass> list; should be vector<BaseClass *> list;
How ever... The point is:
It is somehow not possible to NOT DEFINE the template type!

I do not know the type of the template type T yet. That's why i want to have a template solution!

1
2
//I need simply a solution for this line.
vector<TemplClass<I_DONT_KNOW_YET> *> list;


In java it is: TemplClass<?>
Make Container a template class:
1
2
3
4
5
6
7
8
9
10
11
tamplate <typename T>
class Container{
private:
 vector<TemplClass<T> > list;
public:
 void add(const TemplClass<T>& tcObject){ list.push_back(tcObject); }
 void smthSmthLast(void){
  list.back().smthSmth();
  list.pop_back();
 }
};


Keep in mind that TempClass is not a class but a class template, it doesn't have object code generated if no classes are instancied by using the class with actual template parameters.
C++ templates are very different from JAVA generics and don't work the same way at all.
Last edited on
But then the template classes all use the same type!
I want them to have different types! I guess (because of your last answer, aqaz), it is just not possible?

So i have to do it with my BaseClass solution.

what a pitty....

Merry Christmas everybody
Last edited on
closed account (S6k9GNh0)
seraph, I honestly find that container solutions are rather bad. I'd rather just let the user of my class make a container themselves which generally involves little to no effort and simplifies your original base class design.
Last edited on
Topic archived. No new replies allowed.