Question about templates

Started by
3 comments, last by Wildfire 21 years, 2 months ago
I''ve written my own template class for linked lists (Yes, I know about std::list). Now, the question I have is the following: I''ve splitted the template in two: One ist the list-template, the other is the node-template. What I''m trying to achieve is the following: Write another node-template (with differenct functionality), and then tell the list-template to use either of them. So, instead of creating an instance of list that way: List<objectType> objectList; I want something like: List<objectType,NodeType> objectList; Is there a way to achieve this, and if yes, how? (The idea between different nodes is this: One list actually hold the objects (owns them), the other list has references to objects being owned by something else. The second method would be to simply ''sort'' the objects.)
How do I set my laser printer on stun?
Advertisement
As long as the two templated node classes have the same interface, i.e. function names operator overloads and such it will work. I am not however sure that it is a good idea to do so, I would make two classes deriving from a common base instead. It is a safer and imho much cleaner design.
[Insert cool signature here]
I understand what you''re telling about deriving the node-templates from a common base, the part I don''t get is how I''m going to tell the list which node it should use. Maybe it''s obvious and my brain is just hung in a while(true)-loop right now
How do I set my laser printer on stun?
Template template parameters syntax (I may have it wrong):

    template <typename ObjectType,           template <typename = ObjectType> class NodeType = DefaultNode> class List {    // ... };// Then you should be able to dotypedef List<int> Foo;                     // List<int, DefaultNode<int> >typedef List<int, SpecialNode> Bar;        // List<int, SpecialNode<int> >typedef List<int, DefaultNode<long> > Baz  // List<int, DefaultNode<long> >typedef List<int, SpecialNode<long> > Quux // List<int, SpecialNode<long> >                                           // (yes, it's stupid :))    


(Source : C++ Templates, The Complete Guide, Vandevoorde & Josuttis )

The number of template parameters for NodeType must match exactly that of the classes you're going to use. If you need to access them within the list class, you may name them. The class keyword before NodeType is not interchangeable with typename. The other typenames are.

Alternatively, you could implement your node classes as non-template classes, only manipulating/managing raw memory (void*) , and let your outer List class do the appropriate casts, object creation (using placement new), object destruction (direct destructor call)... That way, you can have a single node class shared for all types of lists, reducing code bloat, and simplifying your template syntax


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on January 31, 2003 3:01:15 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks for the info, I'm going to try it right away.

And, regarding your last comment: I'm trying to do it exactly the other way round. Have one list class, with all types of nodes.
(Nodes with references to objects, nodes that own objects). I'm not really in favour of the casting part...

edit: The difference is the following: If you delete an 'owning'-list, the objects are gone too. If you delete a 'refering'-list, the objects will still be there (owned by their creator).

[edited by - Wildfire on January 31, 2003 3:06:23 AM]
How do I set my laser printer on stun?

This topic is closed to new replies.

Advertisement