C++ template problem -
i implement base class attributes of size know @ compile-time. idea use template base class. following code compiles , runs fine under vc++9.0.
class definition in .h file
template<int n> class baseclass { int* idx; int* incr; int* limit; public: baseclass(void); ~baseclass(void); void loopmethod(void);
};
implementation of class methods in .cpp file
#include "baseclass.h" #include<iostream> using namespace std; // instantiation template class baseclass<2>; template<int n> baseclass<n>::baseclass(void) { idx = new int [n]; incr= new int [n]; limit = new int[n]; for(int m = 0; m < n; m++) { idx[m] = 0; incr[m] = 1; limit[m] = 2; } } template<int n> baseclass<n>::~baseclass(void) { } template<int n> void baseclass<n>::loopmethod( ) { for( idx[n-1]; idx[n-1] < limit[n-1]; idx[n-1] += incr[n-1] ) { cout << "loopmethod nr " << n-1 << " called." << endl; }
}
implementation of main-function:
#include<cstdlib> #include "baseclass.h" using namespace std; int main() { baseclass<2> baseobj; baseobj.loopmethod(); system("pause"); return 0; }
now want nest for-loops loopmethod times size of class attributes. i.e. compiler should generate code write hand
template<int n> void baseclass<n>::loopmethod( ) { for( idx[0]; idx[0] < limit[0]; idx[0] += incr[0] ) { for( idx[1]; idx[1] < limit[1]; idx[1] += incr[1] ) { cout << "loopmethod nr " << 1 << " called." << endl; } cout << "loopmethod nr " << 0 << " called." << endl; } }
anyway, can prompt compiler this, if not declare baseclass template class. code like:
class baseclass { int* idx; int* incr; int* limit; public: baseclass(void); ~baseclass(void); template<int m> void loopmethod(void); };
implementation of class methods in .cpp file
#include "baseclass.h" #include<iostream> using namespace std; template void baseclass::loopmethod<1>(); baseclass::baseclass(void) { idx = new int [2]; incr= new int [2]; limit = new int[2]; for(int m = 0; m < 2; m++) { idx[m] = 0; incr[m] = 1; limit[m] = 2; } } baseclass::~baseclass(void) { } template<int m> void baseclass::loopmethod( ) { for( idx[m]; idx[m] < limit[m]; idx[m] += incr[m] ) { cout << "loopmethod nr " << m-1 << " called." << endl; loopmethod<m-1>(); } } template<> void baseclass::loopmethod<0>(void) { idx[0] = 0; for( idx[0]; idx[0] < limit[0]; idx[0] += incr[0] ) { // cout << "now inner loop executed" << endl; } }
implementation of main-function:
#include<cstdlib> #include "baseclass.h" using namespace std; int main() { baseclass baseobj; baseobj.loopmethod<1>(); system("pause"); return 0; }
but solution searching have template class template method “loopmethod” owing own template parameter tells compiler how many times nest for-loop. have tried various possibilities without success. have suggestion or know solution template problem?
thanks in advance help,
markus.
there lot of issues template:
- what purpose of whole thing?
- why initialising pointers new? know size @ compile time why not make them arrays?
- you not deleting memory allocating
- exception safety if new fails 1 of later arrays
- implementation should in header file unless used few values of n instantiate
- better use classes exist kind of thing, eg boost::array
- refactor out various sections of it.
Comments
Post a Comment