Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Question about templates

This topic is 5771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.)

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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 do

typedef 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]

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!