'C++:Generics >> How to define several template deklarations for a generic function, which serves as an static instance creator
Hiho,
im working on some stuff with instances and wondering, is it possible to create a generic instance creation function for a class/struct and its following derivates. I have 2 Problems and a question for possibility:
- to make it work, that the generic function can at least deliever an instance of the class/struct itself, the funtion is living/existing in. --> got undefined reference error
- to make it work , that the generic function can deliever several types of instances --> got undefined reference type error
- is it possible to make it work, that the generic function deliever types of future derivate types, by impplementing further template declarations in these derivates. So the base class with the generic function dont need to know about them( and by my understanding of the situation, never can , because how knows what future children/derivates will follow )
I prepared a example for better understanding:
//base.hpp
struct base {
base(int param1, int param2) : a(param1), b(param2)
{}
private:
int a,b;
public:
template <typename T> static T createInstanceGeneric(int param1, int param2){
return T(param1, param2);
}
//Thats of course working//
static base createInstance(int param1, int param2) {
return base(param1, param2);
}
void update(int param2){
_update(0, param2);
}
void process() { int tmp = a; a = b; b = tmp }
protected:
void _update(int param1, int param2){
a = param1;
b = param2;
}
}
//END base.hpp
//childOne.hpp
#include "base.hpp"
struct childOne : public base {
childOne(int param1, int param2) : base(param1, param2 ){}
//Thats of course working//
static base createInstanceOfBase(int param1, int param2) {
return base::createInstance(param1, param2);
}
static childOne createInstanceOfSelfViaGeneric(int param1, int param2) {
return base::createInstanceGeneric<childOne>(param1, param2);
}
static base createInstanceOfBaseViaGeneric(int param1, int param2) {
return base::createInstanceGeneric<base>(param1, param2);
}
void update(int param1){
base::_update(param1, 0);
}
}
//END childOne.hpp
//app.cpp
#include "childOne.hpp"
void calledLaterForSomeStuff( const base& data ){
data.process();
}
void main()
{
base test1 = base::createInstance(0,1); //Working
test1.update(2);
calledLaterForSomeStuff(test1);
base test2 = childOne::createInstanceOfBase(0,1); //Working
test2.update(2);
calledLaterForSomeStuff(test2);
//undefined reference to `base base::createInstanceGeneric<base>(int param1, int param2)'//
//undefined reference to `base base::createInstanceGeneric(int param1, int param2)'//
base test3 = childOne::createInstanceOfBaseViaGeneric(0,1); //Question 1 and 2
test3.update(2);
calledLaterForSomeStuff(test3);
//undefined reference to `childOne base::createInstanceGeneric<childOne>(int param1, int param2)'
//undefined reference to `childOne base::createInstanceGeneric(int param1, int param2)'
childOne test4 = childOne::createInstanceOfSelfViaGeneric(0,1); //Question 3
test4.update(3);
calledLaterForSomeStuff(test4);
}
//END app.cpp
Solution 1:[1]
Ok, it works all out of the box. I had the implemententation of the template inside the cpp file. But its only allowed in the hpp.
Now its works.
Case CLOSED
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CAP |
