•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,010 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,894 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2788 | Replies: 5
•
•
Join Date: Jan 2005
Posts: 24
Reputation:
Rep Power: 4
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.
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
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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



Threaded Mode