{"id":148,"date":"2019-01-02T22:55:36","date_gmt":"2019-01-03T06:55:36","guid":{"rendered":"http:\/\/35.243.195.209\/?p=148"},"modified":"2019-01-02T22:55:36","modified_gmt":"2019-01-03T06:55:36","slug":"delegating-and-inheriting-constructors","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/01\/02\/delegating-and-inheriting-constructors\/","title":{"rendered":"Delegating and Inheriting Constructors"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will show delegating constructors introduced in C++ 11.<\/p>\n<h2>Details<\/h2>\n<p>I come across the <a href=\"&quot;https:\/\/leetcode.com\/problems\/peeking-iterator\/discuss\/72598\/Another-C%2B%2B-solution-with-one-line-in-peek()-and-hasNext()-AC&quot;\">codes<\/a> below, and get confused about the systax.<\/p>\n<pre><code class=\"c\">class Iterator {\n    struct Data;\n    Data* data;\npublic:\n    Iterator(const vector&lt;int&gt;&amp; nums);\n    Iterator(const Iterator&amp; iter);\n    virtual ~Iterator();\n    int next();\n    bool hasNext() const;\n};\n\nclass PeekingIterator : public Iterator {\nprivate:\n    int m_next;\n    bool m_hasnext;\npublic:\n    PeekingIterator(const vector&lt;int&gt;&amp; nums) : Iterator(nums) {\n        m_hasnext = Iterator::hasNext();\n        if (m_hasnext) m_next = Iterator::next();\n    }\n\n    int peek() {\n            return m_next;\n    }\n\n    int next() {\n        int t = m_next;\n        m_hasnext = Iterator::hasNext();\n        if (m_hasnext) m_next = Iterator::next();\n        return t;\n    }\n\n    bool hasNext() const {\n        return m_hasnext;\n    }\n};\n<\/code><\/pre>\n<p>After some research, I fully understand the codes.<\/p>\n<h3>1.What does <code>PeekingIterator(const vector&lt;int&gt;&amp; nums) : Iterator(nums) {...}<\/code> do?<\/h3>\n<p>Actually in the past versions of C++, it is not allowed to inherit constructors. Howerver, in C++ 11, we can use delegating constructors, which use the father&#8217;s constructor to initialize inherited member variables.<\/p>\n<p>There is another method using <code>using<\/code>, see the highest voted answer <a href=\"https:\/\/stackoverflow.com\/questions\/347358\/inheriting-constructors\">here<\/a>.<\/p>\n<h3>2.What does <code>Iterator::hasNext()<\/code> do?<\/h3>\n<p>Since we have overided the <code>hasNext()<\/code> function in child class, if we want use the parent&#8217;s member function, we should use <code>Iterator::<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will show delegating constructors introduced in C++ 11. Details I come across the codes below, and get confused about the systax. class Iterator { struct Data; Data* data; public: Iterator(const vector&lt;int&gt;&amp; nums); Iterator(const Iterator&amp; iter); virtual ~Iterator(); int next(); bool hasNext() const; }; class PeekingIterator : public Iterator { private:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":2,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}