Passing dynamically allocated array to Templated Class

This is a discussion on Passing dynamically allocated array to Templated Class within the C++ Programming forums, part of the General Programming Boards category; I have a templated class called Auto. How would the constructor parameter list look, if I'm trying to do this ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Passing dynamically allocated array to Templated Class

    I have a templated class called Auto. How would the constructor parameter list look, if I'm trying to do this in the main.cpp.

    #define NUM_SCORES 10
    Auto< int > num( new int[ NUM_SCORES ] );

    Thanks for the help, I just don't know how to take a dynamic array as a parameter to my constructor.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    new T[ NUM_SCORES ]

    will result in a T* to the first element. If you have a template<T>, then you your constructor should accept a T*.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, I'm not really sure what you mean. I really just need syntax help on the constructor. In the cpp, how would I syntatically write it out?

    template <class _T>Auto<_T>::Auto( ... ) ??

    Also, it said on my above snippet that type 'int' was unexpected. I thought if you said <class _T> that it would accept class,struct, and predefined data types.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    It's hard to be sure without knowing what you're doing with the parameter in the constructor, but based on what you've stated it would look something like -

    template <class _T>Auto<_T>::Auto(_T* array) {
    //blah blah
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2008, 01:10 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Deleting Dynamically Allocated Array Members
    By theJ89 in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2006, 07:37 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Replies: 4
    Last Post: 09-12-2001, 03:05 PM

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