Re: Question About temlates.

From: Joec (joec_at_annuna.com)
Date: 02/28/04


Date: Sat, 28 Feb 2004 04:18:32 GMT


Leor Zolman wrote:
> 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

There is more to the program. My array works. I am trying to create
the class in the most basic way. I can use a vector and in reality I
don't need this program but I am trying to get it to work. Yes just
appending at the end is fine for now until I want to add more functions.
  I did write a simple templat for multiplying 2 numbers. That worked too.
>
>
> .
>
>>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



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: Question About temlates.
    ... >will act like a dynamic array. ... You're describing the right approach to creating a template from a ... a dynamic array generally means that there's at least one ... many elements in the constructor. ...
    (alt.comp.lang.learn.c-cpp)
  • 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: How can I extract variables from a ruby snippet
    ... multi-dimensional array, ... Otherwise, you could execute the template using erb, and use ... methods have been called on that dummy object. ... you pass in a hash of 'local_assigns', ...
    (comp.lang.ruby)
  • 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)