Re: Question About temlates.

From: Leor Zolman (leor_at_bdsoft.com)
Date: 02/26/04

  • Next message: Joec: "Re: [C++] Re: Question About templates."
    Date: Thu, 26 Feb 2004 01:30:04 GMT
    
    

    On Thu, 26 Feb 2004 00:39:02 GMT, Joec <joec@annuna.com> wrote:

    >I am trying to rewrite part of the standard library. Not fully, I am
    >still new at this, but what I want to do is create a template class that
    >will act like a dynamic array. I have gotten it to work with integers
    >but I am having trouble with changing the variables from int to T.

    You're describing the right approach to creating a template from a
    non-template class: first get it to work as a non-template, then turn it
    into a template. The turning-into-a-template part is not much more than
    adding the "template" lines in the appropriate places and changing your
    hard-wired element type to the template parameter name where appropriate.
    But I have a hard time believing you've begun with a "working" dynamic
    array class in this case, because there are so many catastrophic syntactic
    and logical errors, and pieces just plain missing.

    For starters, a dynamic array generally means that there's at least one
    constructor that accepts a size, and you allocate enough space for that
    many elements in the constructor. You may also choose to have a default
    constructor, and you have to decide what the appropriate thing would be for
    it to do.

    There are lots of variations; if you choose to allow the array to "grow"
    like a std::vector, then you might choose to /not/ have the c'tor that
    takes a size, and instead have a rule to dictate how the array grows (by a
    constant or geometric size each time, for example).

    If you implement reading and writing into the array as you're doing with
    "put" and "read" (usually called "get"), then your "put" should accept a
    position and a value to put into it (yours takes just a value; were you
    planning to only be able to append values to the end?)

    All those choices, though, are independent of whether it is a template or
    not. Try really getting a class like that working, then give template-izing
    it a shot and post what you've got if you get stuck.

    Good luck,
            -leor

    .
    >
    >template <class T> class box{
    >
    > int size;
    > T* parray;
    >
    >public:
    >
    > box();
    > box(const box& cbox);
    > ~box();
    > void put(T add);
    > T read(int num);
    > int size();
    > }
    >
    > box::box()
    > size = 0;
    > T* parray = new T[size + 1];
    > }
    >
    >box::box(const box& cbox){
    > cout<<"Copy Constructor...\n";
    > narry = new cls;
    > int nsize = size;
    > *narry = *parry;
    > }
    > ....
    >
    >All I want to do is get a given type and store it in a dynamic array.
    >Where am I going wrong?

    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
               Decryptor at www.bdsoft.com/tools/stlfilt.html


  • Next message: Joec: "Re: [C++] Re: Question About templates."

    Relevant Pages

    • Re: Listing objects
      ... When Template is instantiated, it instantiates Control, which, in turn, instantiates Data. ... Another way would be to have a global array and save all objects there by reference. ...
      (comp.lang.php)
    • Re: nested conditional that can identify parent page?
      ... and specify the files in the array including ... try to make as little php and html mix as possible, have a template ...
      (alt.php)
    • Re: Read a textfile to an array
      ... I need some help with writing a member function really badly. ... >template class with the constructor as follows: ... Are the contents of the file supposed to be stored into the array you ...
      (microsoft.public.vc.language)
    • Re: trouble creating array of objects
      ... > than a 2D array, because you have to put the dynamic allocation inside ... > a class template. ... > happen to be learning because they enjoy the same challenge. ... say advanced template classes but I mean that the use of cout and cin for ...
      (alt.comp.lang.learn.c-cpp)
    • Re: Question About temlates.
      ... Leor Zolman wrote: ... but what I want to do is create a template class that ... >>will act like a dynamic array. ... > many elements in the constructor. ...
      (alt.comp.lang.learn.c-cpp)