Thread: templates

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    templates

    Code:
    template <class t>
    class datalist {
    public:
    	datalist() {
    		name = 0;
    		next = 0;
    	}
    
    	char *name;
    	t *p;
    
    	datalist *next;
    };
    Now how to dynamically allocate new datalist and set *p to char * or something?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by l2u
    Now how to dynamically allocate new datalist and set *p to char * or something?
    Code:
    template <class t>
    class datalist {
       public:
    	datalist() {
    		name = 0;
    		next = 0;
    	}
            void allocateP(int size);
    
       private:
    	char *name;
    	t *p;
    
    	datalist *next;
    };
    
    template <class t>
    void datalist<t>::allocateP(int size) {
       p = new t[size];
    }
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    As I said last time, all of this has already been done for you and is wrapped up in nice, highly used and thoroughly tested libraries freely available to you. Why are you trying to write your own version that will be buggy and require you to fix and maintain it?

    If it's a learning exercise, ok, although I think that learning to use those libraries might be just as if not more beneficial.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved
    As I said last time, all of this has already been done for you and is wrapped up in nice, highly used and thoroughly tested libraries freely available to you. Why are you trying to write your own version that will be buggy and require you to fix and maintain it?

    If it's a learning exercise, ok, although I think that learning to use those libraries might be just as if not more beneficial.
    Probably you're pointing on my *next (linked list).

    I don't like these std:: (vector and stuff) in my code - probably because im not used to it, but if you say these things are much better to use and work with, then I will consider using it. Is it really so much better and faster?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by SlyMaelstrom
    Code:
    template <class t>
    class datalist {
       public:
    	datalist() {
    		name = 0;
    		next = 0;
    	}
            void allocateP(int size);
    
       private:
    	char *name;
    	t *p;
    
    	datalist *next;
    };
    
    template <class t>
    void datalist<t>::allocateP(int size) {
       p = new t[size];
    }
    This will allocate P, but how to allocate new datalist.
    Like you do: datalist *temp = new datalist();

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by l2u
    Probably you're pointing on my *next (linked list).

    I don't like these std:: (vector and stuff) in my code - probably because im not used to it, but if you say these things are much better to use and work with, then I will consider using it. Is it really so much better and faster?
    It's likely significantly better and faster than anything you could write at your current level. Even when/if you get really good, your version will really only be as good as the STL version. So use STL. It's very, very useful, and if you're serious about programming, you'll have to know it, anyway, because even if you don't use it, you'll surely encounter it in code you're maintaining.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is better because the code has already been written, tested and debugged. It makes development time faster (usually even if you have never used it before) because it is easier to use and harder to mess up. In most cases, the speed is basically the same.

    I would start by using std::string and std::vector. They aren't that hard to use, and will make writing this class easier. If you want to try boost::variant, that will do what you want, but it is more advanced and is not standard so you have to download the boost library and put in more effort to learn it.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    What about the program size? Can I make std:: invisible? (Heh)

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The STL components are no bigger than the code you'd have to write for yourself anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you don't like writing std:: everywhere, you can use a using directive, but IMO the benefits of actually writing it outweigh those of not.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    630
    Where can I read some tutorials/documents about using std:: ?

    Thanks again.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by l2u
    Where can I read some tutorials/documents about using std:: ?
    I think we pretty much told you everything you need to know right in this post. Here's the deal, everything in a standard library (such as <iostream>, <string>, <cstdlib>, everything minus .h and inside the angle brackets) is in the std namespace. In order to use their functionality, you either precede every instance of an indentifier from that library with std:: or you put at the top of your program
    Code:
    using namespace std;
    This however is a bad habit as if you put this in a header the using acts in every file that header is included in. If you want a tutorial on namespaces, then this site has one. Just go to the Tutorials and Ctrl-F for "namespace".
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Most C++ tutorials and references will have information on string and vector. I believe this site has some.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by SlyMaelstrom
    I think we pretty much told you everything you need to know right in this post. Here's the deal, everything in a standard library (such as <iostream>, <string>, <cstdlib>, everything minus .h and inside the angle brackets) is in the std namespace. In order to use their functionality, you either precede every instance of an indentifier from that library with std:: or you put at the top of your program
    Code:
    using namespace std;
    This however is a bad habit as if you put this in a header the using acts in every file that header is included in. If you want a tutorial on namespaces, then this site has one. Just go to the Tutorials and Ctrl-F for "namespace".
    There's a thrid option, and it's probably what the OP is looking for. You can include only one item from a namespace:
    Code:
    using std::vector;
    Then you can use just plain "vector", but everything else, like "std::cout" or "std::getline" has to be prefixed with "std::" (unless you add using directives for them too, of course).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by dwks
    There's a thrid option, and it's probably what the OP is looking for. You can include only one item from a namespace:
    Code:
    using std::vector;
    Then you can use just plain "vector", but everything else, like "std::cout" or "std::getline" has to be prefixed with "std::" (unless you add using directives for them too, of course).
    I always hated that option and often block it out from mentioning it. If he was so concerned with reducing the size of his program, then I feel like he doesn't want half-a-dozen or so using directives at the top of his program. Honestly, I don't know why the C standard doesn't let you comma seperate those, but in any given program (that he'll be writing as he learns) he'll have a line for cout, cin, endl, string, getline... and usually one or more of ofstream, ifstream, vector... and all that will likely leave him with is some annoying syntax errors when he realizes he used a few functions that he didn't have a directive for when he compiles. To me, that option is like people who dip their feet in the pool before going in so they don't get too cold. I say, either jump in or don't go in at all.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM