c++ - multiple inheritance without virtual inheritance -


i trying understand multiple inheritance, here code:

struct {   a() {}   static int n;   static int increment() { return ++n; } }; int a::n = 0;  struct b : public {}; struct c : public {}; struct d : public b, c {};  int main() {   d d;   cout<<d.increment()<<endl;   cout<<d.increment()<<endl; } 

this code works. however, if change increment() non-static fail.

my questions:

  1. why compiler complains ambiguous call of non-static version of increment(), while satisfies static one?
  2. if add increment() function b or c, compiler complain too, declared static. why?

what ambiguous mean?

a compiler complains of ambiguous calls when cannot decide function call given context. so, in order understand complaints, have check possible ambiguities be.

why compiler complains ambiguous call of non-static version of increment(), while satisfies static one?

by definition, static function of class not depend on instance of class. emphasized fact call a::increment() (see, no instance).

the problem of diamond inheritance not compiler not know code execute, it's not know this provide (there 2 a in d object, 1 contained in b , 1 in c).

when use static function of a, no implicit this passed, there no issue; if try use non-static function, compiler cannot decide whether this should point a in b or in c, it's ambiguous.

if add increment() function b or c, compiler complain too, declared static. why?

at point, compiler may choose between b::increment() , c::increment(), should pick? it's ambiguous.

when have linear hierarchy, calls "closest" (which hides further down inheritance tree), here b , c 2 independent branches , there no "better" branch.

note: if b not implement increment, since a can call b::increment() calls a::increment(). same goes c.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -