Extending Template Classes - Best Practices

T

Tom

Hi,

I am trying to understand best practices in extending template container
classes. Say one has implemented an effecient parameterised tree
container that does not allow for duplicate values. Let us further
assume the tree method is parameterised by a single type T and a
functional C . The functional defines an order relation on the tree and
is provided a default value say std::less< T >. The tree interface
supports a minimal interface, providing insert, and remove methods. One
would now like to implement a parameterised Set container. Let us
suppose there are two cases here

- The set supports the same interface - insert/remove but with slightly
different implementation from the tree

- The set supports and augmented interface - insert/remove (with no
change in implementation), and additional methods such as union
intersection and complement.

In either case what is the appropriate approach, i.e set composes tree
or set inherits tree or tree is also parameterised by another type
(traits ?) that makes it behave as a set alternatively ?

What would be a good resource (book/website) to learn more about such
issues in developing parameterised classes and complexities of name
lookup rules for parameterised class hierarchies.

regards
Tom
 
A

Alf P. Steinbach

Hi,

I am trying to understand best practices in extending template container
classes. Say one has implemented an effecient parameterised tree
container that does not allow for duplicate values. Let us further
assume the tree method is parameterised by a single type T and a
functional C . The functional defines an order relation on the tree and
is provided a default value say std::less< T>. The tree interface
supports a minimal interface, providing insert, and remove methods. One
would now like to implement a parameterised Set container. Let us
suppose there are two cases here

- The set supports the same interface - insert/remove but with slightly
different implementation from the tree

- The set supports and augmented interface - insert/remove (with no
change in implementation), and additional methods such as union
intersection and complement.

In either case what is the appropriate approach, i.e set composes tree
or set inherits tree or tree is also parameterised by another type
(traits ?) that makes it behave as a set alternatively ?

What would be a good resource (book/website) to learn more about such
issues in developing parameterised classes and complexities of name
lookup rules for parameterised class hierarchies.

At the design level a set is different from a tree.

So the tree stuff should not be publicly available from the set interface.

In practice containment is generally preferable over private or
protected inheritance, but whatever works & feels right (e.g.
convenience) and isn't immediately attacked by code reviewer. :)


Cheers & hth.,
 
J

Jorgen Grahn

Hi,

I am trying to understand best practices in extending template container
classes. Say one has implemented an effecient parameterised tree
container that does not allow for duplicate values. Let us further
assume the tree method is parameterised by a single type T and a
functional C . ....

What would be a good resource (book/website) to learn more about such
issues in developing parameterised classes and complexities of name
lookup rules for parameterised class hierarchies.

I don't see how your problem relates to templates -- are you sure
you're not confusing yourself? Step back and think of the two plain
classes SetOfFoo and TreeOfFoo (for some example type Foo) if you
notice your head beginning to spin. (That's what I do.)

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top