LeetCode 284: Peeking Iterator I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time!
Problem

Given an Iterator class interface with methods: next() and
hasNext(), design and implement a PeekingIterator that support the
peek() operation -- it essentially peek() at the element that will
be returned by the next call to next().
Example:
list: [1,2,3].

Call next() gets you 1, the first element in the list. Now you call
peek() and it returns 2, the next element. Calling next() after that
still return 2.  You call next() the final time and it returns 3, the
last element.  Calling hasNext() after that should return false. 

Follow up: How would you extend your design to be generic and work
with all types, not just integer?

Code
#include &lt;vector&gt;


class PeekingIterator : public Iterator {
    int next_num;
    bool num_has_next;

public:
    PeekingIterator(const std::vector&lt;int&gt; &amp;nums) : Iterator(nums) {
        num_has_next = Iterator::hasNext();

        if (num_has_next) {
            next_num = Iterator::next();
        }
    }

    int peek() const {
        return next_num;
    }

    int next() {
        int curr_next = next_num;
        num_has_next = Iterator::hasNext();

        if (num_has_next) {
            next_num = Iterator::next();
        }

        return curr_next;
    }

    bool hasNext() const {
        return num_has_next;
    }
};

Reference
LeetCode has a template for answering questions. There is usually a class named Solution with one or more public functions which we are not allowed to rename. For this question, the template is:
/*
 * Below is the interface for Iterator, which is already defined for you.
 * **DO NOT** modify the interface for Iterator.
 *
 *  class Iterator {
 *      struct Data;
 *      Data* data;
 *      Iterator(const vector&lt;int&gt;&amp; nums);
 *      Iterator(const Iterator&amp; iter);
 *
 *      // Returns the next element in the iteration.
 *      int next();
 *
 *      // Returns true if the iteration has more elements.
 *      bool hasNext() const;
 *  };
 */

class PeekingIterator : public Iterator {
public:
    PeekingIterator(const vector&lt;int&gt;&amp; nums) : Iterator(nums) {
        // Initialize any member here.
        // **DO NOT** save a copy of nums and manipulate it directly.
        // You should only use the Iterator interface methods.
        
    }
    
    // Returns the next element in the iteration without advancing the iterator.
    int peek() {
        
    }
    
    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    int next() {
        
    }
    
    bool hasNext() const {
        
    }
};


Problem

Solution

Discuss
Passing larger sized data in functions by const &amp;data is a good idea since it does not make a copy. Note that when a parameter is passed by const&amp;, the extra cost dereferencing and fewer opportunities for compile optimizing. You should do this typically when the data is large in size

From your next() function



    int next() {
        int curr_next = next_num;
        num_has_next = Iterator::hasNext();

        if (num_has_next) {
            next_num = Iterator::next();
        }

        return curr_next;
    } ```


What is the use of the if (num_has_next) branch? You are returning a copy of next_num that was copied before any modifications, and since curr_next is not a reference of next_num, any changes to next_num will not effect return curr_next. I think what you might want to do is
int next(){
    next_num = (num_has_next) ? Iterator::next() : NULL;
    return next_num;
} 

ternary operators 
Since @hjpotter92 has mentioned it,  calling next() or peek() on the last element should return NULL.

Have a look at templates in C++ if you want to work with multiple types.