User Name Password Register
DaniWeb IT Discussion Community
All
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: crq is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

template array initialization

  #1  
Jan 27th, 2005
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.

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
AddThis Social Bookmark Button
Reply With Quote  

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC