templates+member function pointers, please help...
Hey
This code creates an array with pointers to member functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <string>
using namespace std;using namespace std;
class foo
{
int value;
public:
int showRecord(void);
foo(int x) :value(x){}
};
int foo::showRecord(void){ cout << value; return value; }
int main()
{
int(foo::*pt[1])(void) = {& foo::showRecord }; //array with pointers to member functions
foo *da = new foo(15);
(da->*pt[0])(); //same as da->showRecord
delete da;
cin.ignore();
return 0;
}
|
now I want to use templates to make it more general...
I just learned templates ... please help
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 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
using namespace std;
template<class tvar>
class foo
{
tvar value;
public:
tvar showRecord(void);
foo(tvar x) :value(x){}
};
template<class tvar>
tvar foo<tvar>::showRecord(void){ cout << value; return value; }
int main()
{
foo <int>* da = new foo <int> (15) ;
da ->showRecord();// this code must be replaced
//<int>(foo::*pt[1])(void) = { &foo::showRecord };//doesn't work
/* int(foo::*pt[1])(void) = {& foo::showRecord };
foo *da = new foo(15);
(da->*pt[0])();*/
delete da;
cin.ignore();
return 0;
}
lines 22-26 don't work.
|
1 2 3 4
|
foo <int>* da = new foo <int> (15) ;
//da ->showRecord();
int(foo<int>::*pt[1])(void) = { &foo<int>::showRecord };
(da->*pt[0])();
|
problem solved
Topic archived. No new replies allowed.