c++ - Who's function get called when calling f1() through Derived::f2()? -


#include <iostream> #include <string> using namespace std;  class base { public:  base(const string& s): str(s) {cout<<"base::ctor\n";}  base(const base& b): str(b.str) {cout<<"base::copy ctor\n";}  virtual ~base() {cout<<"base::dtor\n";}  void f1() {cout<<"base::f1()\n"; f2();} //2 orders  virtual void f2() {cout<<"base::f2()\n";}  private:  string str; };  class derived : public base { public:  derived(const string& s): base(s)  {cout<<"derived::ctor\n";}  derived(const derived& d): base(d)  {cout<<"derived::copy ctor\n";}  ~derived() {cout<<"derived::dtor\n";}   virtual void f1() {cout<<"derived::f1()\n"; f2();}  void f2() {cout<<"derived::f2()\n"; f1();} //jumps here leaf's f1() };  class leaf : public derived { public:  leaf(const string& s): derived(s)  {cout<<"leaf::ctor\n";}  leaf(const leaf& dd): derived(dd)  {cout<<"leaf::copy ctor\n";}  ~leaf() {cout<<"leaf::dtor\n";}  void f1() {cout<<"leaf::f1()\n"; f3();}  void f3() {cout<<"leaf::f3()\n";} };   int main() {  leaf * p = new leaf("hello");  base * p2 = new leaf(*p);   p2->f1();   delete p2;  delete p;  return 0;  } 

hello,

this question exam phrased 1 it's hard me find right way describe , online.

in line :

p2->f1();  

the output is:

 base::f1()  derived::f2()  leaf::f1()  leaf::f3() 

in derived f2() there's call f1(). who's going called? f1() of type base or f1() of leaf? have been taught, compiler looks function in type left. (base* p2 = new leaf(*p) ) on here can see goes f1() of class leaf. can see leaf's don't understand why...

thanks helpers !

to answer question quickly: derived::f1() called in derived::f2().

to understand why it's derived::f1() that's called, need knowledge of "c++ name hiding in inheritance", can refer online articles like:

you need knowledge of "unqualified name lookup" can refer "member function definition" section in web page: unqualified name lookup.

in summary, major points are:

  • in case of code, derived::f1() hides base::f1() means base::f1() not visible members of derived.
  • the call f1() unqualified name lookup case.
  • names looked inside scope outside. when f1() called in derived::f2(), compiler first looks in inside scope, derived::f2() body itself; entire class derived scope. because f1() can found in scope of derived, becomes 1 that's called.
  • you might think base::f1() looks sitting in same level derived::f1() because of inheritance , wonder why base::f1() not called. recall name hiding.

the process of call should follows:

  1. in main(), p2->f1(); executed.
  2. because p2 pointer base, name of "f1" searched in base's method list.
  3. note base::f1() not virtual, base::f1() called ("base::f1()"). yes, f1() declared virtual in derived, doesn't affect virtual table of base.
  4. base::f1() calls f2 virtual method of base. because f2 overridden in derived, derived::f2() 1 that's called("derived::f2()").
  5. derived::f2() calls f1() derived::f1(). because derived::f1() declared virtual , overridden in leaf, leaf::f1() called("leaf::f1()").
  6. leaf::f1() calls f3() leaf::f3(). f3() method leaf has called("leaf::f3()").

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -