How do you initialize a template class object?

This is a discussion on How do you initialize a template class object? within the C++ Programming forums, part of the General Programming Boards category; I have a templated array class and I want to write an init routine for it but I'm not sure ...

  1. #1
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Unhappy How do you initialize a template class object?

    I have a templated array class and I want to write an init routine for it but I'm not sure how to do that since I don't pass it any particular data type. Any ideas?

    Michael:

    confused:

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What do you intending on doing in you 'init' routine?

  3. #3
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Wink

    I want to initialize all the elements to zero or an equivalent for a template....

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Your array class shouldn't have to worry about how each type represents 0. The type (whatever it may be) should implement that in a way which is invisible to outsiders (eg. Default Constructor)

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You do it the same way as if the array was not templated, however, I recommend that if you are talking about initalizing the ELEMENTS to 0 by default, then think otherwise, because while that would be fine for primitive datatypes, all of the user-made datatypes that you, or other people, create, must have an implicit constructor that takes the single parameter 0, which is pretty limiting.

    Anyways, the syntax for initialization is exactly the same, I'm not sure where your problem is.

  6. #6
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Cool

    So let's say my template class is this...

    template <class T>
    class RArray {
    private:
    typedef enum { StartArraySize = 128} bogus;
    typedef enum {InitialSize = 0};

    size_t num_elem;

    size_t array_size;

    size_t array_Cursor;

    T *pArray;


    private:

    public:


    I can add an init routine like this?


    RArray()
    {
    pArray = new T[ StartArraySize ];
    num_elem = InitialSize;
    array_Cursor = InitialSize;
    array_size = StartArraySize;
    }



    void Init()
    {
    int i = 0;
    for (;i< StartArraySize;i++)
    {
    pArray[i] = 0;
    }
    pArray
    }


    ~RArray()
    {
    if (pArray != NULL) {
    delete [] pArray;
    }
    }

    Last edited by adonisv; 02-12-2003 at 06:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 11:17 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21