c++ - Overriding a private function, how is it different from protected? -


in following program, if getpart() made protected instead of private, there difference outside (derived) classes or additional member functions within these classes? i.e. there compilation error caused having private wouldn't exist if function protected in base class?

i discovered possible override private virtual functions recently, surprised me. semantically seems (to me) job of protected, rather private.

#include <iostream>  class { public:     a() {}     virtual ~a() {}      virtual void runfn() { getpart(); }  private:     virtual void getpart() = 0; };  class b : public { public:     b() {}     virtual ~b() {}  private:     virtual void getpart() override { std::cout << "getpart run" << std::endl; } };  int main() {     b b;     b.runfn();     return 0; } 

see http://ideone.com/s9681v show line run, function gets overriden properly.

this topic creates lot of confusion: though subclasses allowed override virtual private member functions, not allowed call them.

currently, not compile (demo 1):

class b : public { public:     b() {}     virtual ~b() {} private:     virtual void getpart() override {         // line not compile         a::getpart();         std::cout << "getpart run" << std::endl;     } }; 

making getpart() function protected let above code compile without issue, require provide definition (demo 2).

this difference.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -