Sunday 21 June 2020

Chapter 14 // Exercise 15 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the header file found here:
http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 14 // Exercise 15

Most class hierarchies have nothing to do with graphics. Define a class Iterator with a pure virtual function next() that returns a double* (see Chapter 17). 

Now derive Vector_iterator and List_iterator from Iterator so that next() for a Vector_iterator yields a pointer to the next element of a vector<double> and List_iterator does the same for a list<double>

You initialise a Vector_iterator with a vector<double> and the first call of next() yields a pointer to its first element, if any. If there is no next element, return 0. Test this by using a function void print(Iterator&) to print the elements of a vector<double> and a list<double>.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Exercise%2015


Damn, he really just threw us a curve ball with this one. Iterator is actually already taken though so I renamed it My_Iterator.

The most horrible thing about this example though is that it uses concepts that haven't been introduced yet. Sure he's briefly mentioned pointers and we all know what a reference is but lists?? Returning a specific element in a list as a pointer?? I didn't know how to do these things and it's good thing I have to deal with Pointer Engine 4 at work otherwise I can imagine 2018 me staring at stackoverflow answers in confusion for days.

Anyway, I'll go through this:
Chapter 14 // Exercise 15 - Principles & Practice Using C++

The Vector_Iterator is relatively simple. I use a static int to keep track of which element we are up to as static variables don't lose scope when their functions are closed. 

The only confusing part is line 43. &v_double[i++] essentially returns a pointer to where the specific element is. We don't want to return the actual element value which we would be doing if not for the &. A pointer is an address of a variable on your computer. 

You've probably seen post-fix ++ quite a bit in code online however it's very different to pre-fix ++ (which I personally prefer). Legendary programmer Scott Meyers in Effective C++ (excellent book) also advises to "prefer pre-fix increment unless the behaviour of post-fix is specifically needed". This is because post-fix creates a temporary variable, returns that, then increments and applies it back to the original variable. Here, the post-fix is specifically needed as if 'i' is incremented before then we can go out of range and we can't increment after a return.

The List Iterator is more confusing as I've never used a list before. They are doubly linked lists and behave completely different to a vector or array. The most annoying thing about them is that you can't access the elements using the subscript [] operator. There are a few ways to access specific elements of a list but you always have to start at the first element and then go forward N elements. I decided to use the standard library function advance() which takes in a list iterator and an int. The iterator will then become a pointer to the element we want at N.

The worst part is line 70. We need to return a pointer so & is needed. However, the iterator itself is also a pointer so we need to get the actual value by using a *. The & then returns a pointer to the de-ref value...I think. Either way, it's very confusing and this should not have been introduced at this stage...or I've completely over-complicated this.

No comments:

Post a Comment