inheritance and templates

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
 
T

Thomas J. Gritzan

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> > {

You should inherit from the same type as you use four lines later: A<T,U>

You made it more complex than it is.
 
T

tirzanello

You should inherit from the same type as you use four lines later: A<T,U>

it works!!!! it works!!!!

thanks a lot Thomas!!!

cheers
tirzanello
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top