Re: Question About temlates.
From: Joec (joec_at_annuna.com)
Date: 02/28/04
- Next message: Joec: "Re: Question About temlates."
- Previous message: Gary: "Re: pointer/ref question"
- In reply to: Leor Zolman: "Re: Question About temlates."
- Next in thread: Leor Zolman: "Re: Question About temlates."
- Reply: Leor Zolman: "Re: Question About temlates."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Joec: "Re: Question About temlates."
- Previous message: Gary: "Re: pointer/ref question"
- In reply to: Leor Zolman: "Re: Question About temlates."
- Next in thread: Leor Zolman: "Re: Question About temlates."
- Reply: Leor Zolman: "Re: Question About temlates."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|