In this exercise I am using Visual Studio 2017 and a modified version of the std_lib_facilities header found here.
Chapter 19 // Exercise 4
Modify class Link from section 19.9.3 to be a template with the type of value as the template argument. Then redo exercise 13 from Chapter 17 with Link<God>.
I considered removing things that didn't make sense; like addOrdered() as Link can now be any type, how do we determine what a logical order is for any given type? Especially custom ones but then I remembered operator overloading. If we had Link<int>, addOrdered() would add it in numerical order as the check is is link1 less than link2. A string would be added in alphabetical order etc, so I added a helper to God that allows for a less than check against two Gods, returning the one whose name comes before the other. I also added another one to check if Gods were equal.
The find() function is a bit messy as you have to pass in a God but it keeps it nice and generic for lots of other types.
The main takeaway from this exercise though was learning that templated class member functions must be implemented in the header file; if you put them in the cpp it will fail when linking. (Or they must be implemented in the same file the class is declared).
No comments:
Post a Comment