inheritance - Diamond of death and Scope resolution operator (c++) -


i have code (diamond problem):

#include <iostream> using namespace std;  struct top {     void print() { cout << "top::print()" << endl; } };  struct right : top  {     void print() { cout << "right::print()" << endl; } };  struct left : top  {     void print() { cout << "left::print()" << endl; } };  struct bottom: right, left{};  int main() {     bottom b;     b.right::top::print(); } 

i want call print() in top class.

when try compile error: 'top' ambiguous base of 'bottom' on line: b.right::top::print(); why ambiguous? explicitly specified want top right , not left.

i don't want know how it, yes can done references, virtual inheritance, etc. want know why b.right::top::print(); ambiguous.

why ambiguous? explicitly specified want top right , not left.

that intent, that's not happens. right::top::print() explicitly names member function want call, &top::print. not specify on subobject of b calling member function on. code equivalent conceptually to:

auto print = &bottom::right::top::print;  // ok (b.*print)();                             // error 

the part selects print unambiguous. it's implicit conversion b top that's ambiguous. you'd have explicitly disambiguate direction you're going in, doing like:

static_cast<right&>(b).top::print(); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -