Re: Question About temlates.
From: Leor Zolman (leor_at_bdsoft.com)
Date: 02/26/04
- Previous message: Joec: "Re: Game updated."
- In reply to: Joec: "Question About temlates."
- Next in thread: Francis Glassborow: "Re: Question About temlates."
- Reply: Francis Glassborow: "Re: Question About temlates."
- Reply: Joec: "Re: Question About temlates."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Joec: "Re: Game updated."
- In reply to: Joec: "Question About temlates."
- Next in thread: Francis Glassborow: "Re: Question About temlates."
- Reply: Francis Glassborow: "Re: Question About temlates."
- Reply: Joec: "Re: Question About temlates."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|