Effective Programming Techniques – task 4 Template classes Templates allow you to reuse…
- User
Effective Programming Techniques – task 4 Template classes Templates allow you to reuse the same code multiple times. This is convenient when you create functions and classes that are to be used, for example, to store some values. A typical and often used class template is vector. This class can be used to store any types, but the stored type must be specified when creating the vector. It is important that the logic of operations performed by vector is not dependent on the stored type. It can therefore be written without knowing the stored type. In C++, in addition to class templates, you can also declare function templates, but in this exercise we will focus only on classes. An example template that creates an array of type T is shown below. Template declaration, type T is a necessary parameter. Its definition is required when our program wants to use the CTable class. template< typename T > class CTable { public: CTable() { i_size = 0; pt_table = NULL; } ~CTable() { if (pt_table != NULL) delete [] pt_table; } bool bSetLength(int iNewSize); T* ptGetElement(int iOffset); bool bSetElement(int iOffset, T tVal); private: int i_size; T pt_table; };//template< typename T > class CTable In the case of template types in C++, due to linker error visibility, method implementations are usually placed in header files below the class declaration. This is because the compiler needs to know how to generate a given function for a specific type T. In the case of the CTable class, the method definitions will be as follows. „ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 2/7 template bool CTable::bSetLength(int iNewSize) { if (iNewSize <= 0) return(false); T pt_new_table; pt_new_table = new T[iNewSize]; if (pt_table != NULL) { int i_min_len; if (iNewSize < i_size) i_min_len = iNewSize; else i_min_len = i_size; for (int ii = 0; ii < i_min_len; ii++) pt_new_table[ii] = pt_table[ii]; delete[] pt_table; }//if (pt_table != NULL) pt_table = pt_new_table; return(true); }//bool CTable::bSetLength(int iNewSize) template T CTable::ptGetElement(int iOffset) { if ((0 <= iOffset) && (iOffset < i_size)) return(NULL); return(&(pt_table[iOffset])); }//T CTable::ptGetElement(int iOffset) template bool CTable::bSetElement(int iOffset, T tVal) { if ((0 <= iOffset) && (iOffset < i_size)) return(false); pt_table[iOffset] = tVal; return(true); }//bool CTable::bSetElement(int iOffset, T tVal) Please note that the behavior of the CTable class is the same whether the type of T is int, double, class, or pointer (single or multiple). „ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 3/7 The use of the CTable class can be as follows. int i_template_test() { CTable c_tab_int; CTable<double *> c_tab_double_point; c_tab_int.bSetLength(10); c_tab_int.bSetElement(1, 22); int i_val = *(c_tab_int.ptGetElement(1)); double d_my_doub = 5; c_tab_double_point.bSetLength(2); c_tab_double_point.bSetElement(1, &d_my_doub); *(c_tab_double_point.ptGetElement(1)) = 5; }//int i_template_test() Creating methods dedicated to a given template type Sometimes we would like some template types to behave differently, depending on the type of the parameter. Method specialization is achieved in the following way. We add the sGetKnownType method to the CTable class. template< typename T > class CTable { public: CTable() { i_size = 0; pt_table = NULL; } ~CTable() { if (pt_table != NULL) delete [] pt_table; } bool bSetLength(int iNewSize); T ptGetElement(int iOffset); bool bSetElement(int iOffset, T tVal); CString sGetKnownType(); private: int i_size; T *pt_table; };//template< typename T > class CTable The implementation of the sGetKnownType method looks as follows. template CString CTable::sGetKnownType() { CString s_type = "Unknown"; return(s_type); }//CString CTable::sGetKnownType() template <> CString CTable::sGetKnownType() { CString s_type = "INT"; return(s_type); }//CString CTable::sGetKnownType() „ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 4/7 Example of use: int i_template_test() { CTable c_tab_int; CTable<double *> c_tab_double_point; c_tab_int.bSetLength(10); c_tab_int.bSetElement(1, 22); int i_val = *(c_tab_int.ptGetElement(1)); double d_my_doub = 5; c_tab_double_point.bSetLength(2); c_tab_double_point.bSetElement(1, &d_my_doub); type will be used. „ZPR PWr – Zintegrowany Program Rozwoju Politechniki Wrocławskiej” 5/7 Task NOTES:
- Assistant
- User
||=== Build: Debug in task4 (compiler: GNU GCC Compiler) ===| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|19|error: 'E' was not declared in this scope| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|19|error: found ':' in nested-name-specifier, expected '::'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|19|error: 'error' has not been declared| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|22|error: expected primary-expression before '}' token| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|21|error: expected ';' before '}' token| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|22|error: expected primary-expression before '}' token| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|21|error: expected ')' before '}' token| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|19|note: to match this '('| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\Saver.h|22|error: expected primary-expression before '}' token| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\main.cpp||In function 'int main()':| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\main.cpp|7|error: missing template arguments before 'saver'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\main.cpp|12|error: 'saver' was not declared in this scope| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\main.cpp|12|note: suggested alternative: 'Saver'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\main.cpp|14|error: no matching function for call to 'CResult<void, CError>::CResult(CError)'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|41|note: candidate: 'CResult<void, E>::CResult(const CResult<void, E>&) [with E = CError]'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|41|note: no known conversion for argument 1 from 'CError' to 'const CResult<void, CError>&'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|40|note: candidate: 'CResult<void, E>::CResult(std::vector<E*>&) [with E = CError]'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|40|note: no known conversion for argument 1 from 'CError' to 'std::vector<CError*, std::allocator<CError*> >&'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|39|note: candidate: 'CResult<void, E>::CResult(E*) [with E = CError]'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|39|note: no known conversion for argument 1 from 'CError' to 'CError*'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|38|note: candidate: 'CResult<void, E>::CResult() [with E = CError]'| C:\Users\USER\OneDrive\Desktop\c++\uni\task4\CResult.h|38|note: candidate expects 0 arguments, 1 provided| ||=== Build failed: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
- Assistant