Situation: there is an abstract class (names from the bald) Father, the class Sister inherited from it and just the class Child. In Child there is a Father * mom, in which the constructor writes the address of Sister. Father has a pure virtual method getName (), which is overridden in Sister. Question: how can I from Child call the Sister's getName () method, say from some foo () method?

void foo() { mom->getName(); //не сработает, и если указать Mom::, даже если есть доступ } 

If not, then how is it possible? And what are the alternatives? Thank!

    2 answers 2

    Yes, as usual ... As you can see, mom->getName(); works fine.

     class Father { public: virtual void getName() = 0 { cout << "Father\n"; } }; class Sister: public Father { public: void getName() override { cout << "Sister\n"; } }; class Child { public: Child():mom(new Sister){} ~Child(){ delete mom;} void foo() { mom->getName(); } private: Father * mom; }; int main(int argc, const char * argv[]) { Child c; c.foo(); } 

    Destructors for brevity did not define; in a good way, we need virtual destructors Father and Sister .

    • The joke is that such a model does not work for me, in Qt :(, I would be very grateful if you glanced, was already tortured github.com/Xambey/QSnake , the hierarchy is the same, there is SnakeBase = Farher, Snake = Sister, ItemSnake = Child - Xambey
    • @Xambey Qt I do not do, there is no car, so there is no place to compile and check. If there is a minimal example without using Qt ... - Harry
    • cyberforum.ru/qt/thread1779395.html#post9363603 here is a description, in Qt for this you do not need to understand, where I messed up from the point of view of the PLO? - Xambey
    • @Xambey show compiler errors. - gbg
    • one
      @Xambey there you just baby blots. Take pull-request. - gbg

    Lead types:

     auto Sist=dynamic_cast<Child*>(mom); if(Sist) { Sist->getName(); } 

    The if(Sist) check is necessary in order that if a mom not of type Child (and this can only be found in runtime), do not create UB.

    Father virtual method:

     class Father { public: virtual string getName() const=0; } class Child: public Father { const string name; public: virtual string getName() const { return name; } } 
    • OO, but in another way? I was just surprised: according to the idea from the point of view of the compiler, he sees: so here we have one method with that name in abc, is in derivative, can't he explicitly indicate which class method to use? - Xambey
    • Judging by your code, then consider what I call getName from Sister - Xambey
    • And if then another heir will be determined? and used as mom ? The feeling that there is a decision in the spirit of "why just, if you can be difficult." Anyway, because mom = new Father impossible. - Harry
    • @Harry either do the homology of classes, as the author did (it’s not clear why it doesn’t work for him), or we use RTII. RTII is good because it allows you to call different methods depending on the type of heir. - gbg