title: C++ - Return in Recursion of Tree
body: <pre><code>class Person {
  private:
      char* name;
      int numChildren;
      Person** childrenList;
  public:
      Person(char* name);
      ~Person();
      // member functions
          // ...
  };
  </code></pre>
  
  <blockquote>
    <p>Suppose I create a <code>Person</code> by this code: <code>Person* me = new Person("Alex");</code>, <code>Alex</code>'s descendants' object will also be created. For a specific instance, the structure will be like this: <a href="https://i.stack.imgur.com/XkpJZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XkpJZ.jpg" alt="enter image description here"></a></p>
  </blockquote>
  
  <p>I'd like to implement a function <code>const Person* Person::findPerson(const Person* thisPerson, const char* target_name) const;</code>, that searches through all <code>Person</code> to find the one with <code>name</code> equals <code>target_name</code>.<br>
  Here is my code:</p>
  
  <pre><code>const Person* Person::findPerson(const Person* thisPerson, const char* target_name) const {
      if (strcmp(thisPerson-&gt;name(), target_name) == 0)
          return thisPerson;
      for (int i = 0; i &lt; thisPerson-&gt;numChildren(); i++)
          findPerson(thisPerson-&gt;childrenList[i], target_name);
  }
  </code></pre>
  
  <p>I think the code will work but the compiler thinks it might returns nothing and don't let me compile it. And also, I don't know how to return <code>nullptr</code> if nobody is found.</p>