T
tirzanello
Hi guys,
I'm becoming crazy with templates, maybe for it's a trivial problem...
but I can't go through :-(
any hint will be appreciated.
I show you without templates a piece of code: 2 classes, one inherited
by the other, like these:
#include <iostream>
using namespace std;
class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
int a; int b;
};
class B: public A {
public:
B(){ c=30; }
void f(){cout <<endl<< "result:" << a << " "<< " "<< b << " " << c;
};
int c; int d;
};
//and then the main with a pointer to the class base.
int main(int argc, char **argv){
A * aPtr = new B();
aPtr->f();
aPtr->g();
delete aPtr;
return 0;
}
it works great!!
Now suppose that I need "a" and "c" of a generic type T and "b" and "d"
of a generic type U.
And now, begans the problem!
I wrote this (it compiles):
template <typename T, typename U>
class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
T a; U b;
};
template <typename T, typename U>
class B: public A < B<T,U>, B<T,U> > {
public:
B(){ c=30;}
void f(){
cout <<endl<< "result:" << A<T,U>::a << " "<< " "<< A<T,U>::b <<
" " << c;
};
T c; U d;
};
but I really don't know how to declare the functions in the main, in a
similar way as I did for a non-template version. I've tried several
things, even monsters like:
int main(int argc, char **argv){
A<int,int> * aPtr;
aPtr = new B< <int,int>, <int,int> > ();
aPtr->f(); aPtr->g();
delete aPtr;
return 0;
}
nothing, I don't know,
do you have any hint?
thanks a lot,
Tirzanello
I'm becoming crazy with templates, maybe for it's a trivial problem...
but I can't go through :-(
any hint will be appreciated.
I show you without templates a piece of code: 2 classes, one inherited
by the other, like these:
#include <iostream>
using namespace std;
class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
int a; int b;
};
class B: public A {
public:
B(){ c=30; }
void f(){cout <<endl<< "result:" << a << " "<< " "<< b << " " << c;
};
int c; int d;
};
//and then the main with a pointer to the class base.
int main(int argc, char **argv){
A * aPtr = new B();
aPtr->f();
aPtr->g();
delete aPtr;
return 0;
}
it works great!!
Now suppose that I need "a" and "c" of a generic type T and "b" and "d"
of a generic type U.
And now, begans the problem!
I wrote this (it compiles):
template <typename T, typename U>
class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
T a; U b;
};
template <typename T, typename U>
class B: public A < B<T,U>, B<T,U> > {
public:
B(){ c=30;}
void f(){
cout <<endl<< "result:" << A<T,U>::a << " "<< " "<< A<T,U>::b <<
" " << c;
};
T c; U d;
};
but I really don't know how to declare the functions in the main, in a
similar way as I did for a non-template version. I've tried several
things, even monsters like:
int main(int argc, char **argv){
A<int,int> * aPtr;
aPtr = new B< <int,int>, <int,int> > ();
aPtr->f(); aPtr->g();
delete aPtr;
return 0;
}
nothing, I don't know,
do you have any hint?
thanks a lot,
Tirzanello