| | |
template array initialization
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 24
Reputation:
Solved Threads: 0
hey folks,
i have a template created that holds an array of type T elements. my problem is this ...
i am converting this code from a souped up Array class that takes ints as it's elements to this template Array<T> class that will hold T elements. in the original class, i knew that i was going to be dealing with ints as the elements. so i had a default constructor and a copy constructor that both called a private init method to initialize the array depending on what was passed in.
so how do i conver this to make it work for type T? i can't initialize the elements to 0 ...
thanks
crq
i have a template created that holds an array of type T elements. my problem is this ...
i am converting this code from a souped up Array class that takes ints as it's elements to this template Array<T> class that will hold T elements. in the original class, i knew that i was going to be dealing with ints as the elements. so i had a default constructor and a copy constructor that both called a private init method to initialize the array depending on what was passed in.
C++ Syntax (Toggle Plain Text)
void IntArray:: init ( const int *data, int sz) { assert( (0 <= sz) && (sz <= MAXINT)); d_num_elements = sz; if ( 0 == d_num_elements ){ // if no elements in array d_array = NULL; // array points to NULL } else{ d_array = new int [d_num_elements]; // allocate memory // if new returns NULL then no // more available memory assert ( NULL != d_array ); } for (int i=0; i < sz; i++ ){ if ( NULL == data ){ self [i] = 0; } else{ self[i] = data[i]; } } }
so how do i conver this to make it work for type T? i can't initialize the elements to 0 ...
thanks
crq
C++ Syntax (Toggle Plain Text)
void IntArray:: init ( const T *data, int sz) { assert( (0 <= sz) && (sz <= MAXINT)); d_num_elements = sz; if ( 0 == d_num_elements ){ // if no elements in array d_array = NULL; // array points to NULL } else{ d_array = new T [d_num_elements]; } for (int i=0; i < sz; i++ ){ if ( NULL == data ){ self[i] = T(); } else{ self[i] = data[i]; } } }
This is a very bad idea for two reasons. First, assert is meant for debugging program errors. Memory allocation failing is not a program error, it's a runtime error that needs to be addressed other than abruptly terminating and giving the user a cryptic error message designed for programmers. Your first assert is good (though an exception would be better, see below), but your second is not. Also, classes should NEVER terminate the program. Throw an exception and have the calling application decide what to do. Finally, new doesn't return a null pointer by default anymore, it throws a bad_alloc exception. Once again, this is something that the calling application needs to handle, so you can just let the exception propagate.
I'll assume that your operator[] checks for d_array being null, otherwise the loop has a bug.
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 24
Reputation:
Solved Threads: 0
i have 2 constructors that use this function
since the default constructor assigns the number of elements, there would be a NULL declared for the array. i just don't know what to put in the init() method (for either of these) to make the initialization work for a template. i can't initialize everything to 0 becuase what i pass in MAY not be ints. i can't initialize all the T elements of the array to NULL can i?
thanks
crq
C++ Syntax (Toggle Plain Text)
template<class T> Array<T>:: Array ( int sz ) { assert ( 0 <= sz <= MAXINT ); cout << "!!! IntArray default constructor called" <<endl; init ( NULL, sz ); } template<class T> Array<T>:: Array ( const Array<T>& x ) { cout << "!!! IntArray copy constructor called" <<endl; init ( x.d_array, x.size() ); }
since the default constructor assigns the number of elements, there would be a NULL declared for the array. i just don't know what to put in the init() method (for either of these) to make the initialization work for a template. i can't initialize everything to 0 becuase what i pass in MAY not be ints. i can't initialize all the T elements of the array to NULL can i?
thanks
crq
•
•
•
•
Originally Posted by Narue
>assert ( NULL != d_array );C++ Syntax (Toggle Plain Text)
void IntArray:: init ( const T *data, int sz) { assert( (0 <= sz) && (sz <= MAXINT)); d_num_elements = sz; if ( 0 == d_num_elements ){ // if no elements in array d_array = NULL; // array points to NULL } else{ d_array = new T [d_num_elements]; } for (int i=0; i < sz; i++ ){ if ( NULL == data ){ self[i] = T(); } else{ self[i] = data[i]; } } }
This is a very bad idea for two reasons. First, assert is meant for debugging program errors. Memory allocation failing is not a program error, it's a runtime error that needs to be addressed other than abruptly terminating and giving the user a cryptic error message designed for programmers. Your first assert is good (though an exception would be better, see below), but your second is not. Also, classes should NEVER terminate the program. Throw an exception and have the calling application decide what to do. Finally, new doesn't return a null pointer by default anymore, it throws a bad_alloc exception. Once again, this is something that the calling application needs to handle, so you can just let the exception propagate.
I'll assume that your operator[] checks for d_array being null, otherwise the loop has a bug.
>i can't initialize everything to 0 becuase what i pass in MAY not be ints.
I don't see what your problem is. If you read my answer you'll see that I used the default constructor for T (which works for built in types as well) as the replacement for 0. Did you try it? Or did you just assume that I haven't got a clue and proceed to give me irrelevant code snippets with the same question again?
I don't see what your problem is. If you read my answer you'll see that I used the default constructor for T (which works for built in types as well) as the replacement for 0. Did you try it? Or did you just assume that I haven't got a clue and proceed to give me irrelevant code snippets with the same question again?
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>i can't initialize everything to 0 becuase what i pass in MAY not be ints.
I don't see what your problem is. If you read my answer you'll see that I used the default constructor for T (which works for built in types as well) as the replacement for 0. Did you try it? Or did you just assume that I haven't got a clue and proceed to give me irrelevant code snippets with the same question again?
thanks
crq
![]() |
Similar Threads
- Array Initialization (C++)
- Functions and Array help (C++)
- Help : Pointers to array of class objects . (C++)
- template issues - need expert debugger! (C++)
Other Threads in the C++ Forum
- Previous Thread: Secure File Transfer Method with C++???
- Next Thread: Using Division Operator
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






