Template Array Class

This is a discussion on Template Array Class within the C++ Programming forums, part of the General Programming Boards category; Be careful what you ask for. Here's my idea. Remember, the computer may do things fast, but it is still ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Be careful what you ask for. Here's my idea.

    Remember, the computer may do things fast, but it is still a dumb machine. The creators of the language decided that a null terminated char array should be something different than a non-null terminated char array and that it should be handled in a different, and hopefully, more user friendly way. For one thing, they overloaded the >> and the << operators so that you can output the non-null chars in a null terminated char array and read in all the characters plus add the null-terminating characted in one fell swoop rather than having to input each character individually like you did with the ints in test, test2, and test3 or display each character in the string with a separate cout statement like you did for each int in Array<int>.

    You can probably input individual chars into Array<char> if you want, like you did for Array<int>, but it may be more difficult to use C-style strings (null terminated char arrays) as type T in Array<T>. Differentiating between null-terminated char arrays and non-null terminated char arrays so they can be two separate instances of type T and therefore handled separately as they are in all code outside Array, will not be straight forward, if possible at all.

    On further contemplation I'll add this.

    Some templated classes, like the STL set class, require that any T have certain properties. For sets, that means that any class T that is used to create type Array<T> has to be comparable using the less than operator, which is the default comparator, or some other comparator, if you don't use the default. Therefore, you can't use C-style strings (aka null terminated char array) as class T in STL sets that use the default comparator. You may find you need to impose the restrictions on user input for your Array class.

    But that's just my thought.
    Last edited by elad; 04-12-2004 at 08:25 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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