22

Why does this happen?

http://coliru.stacked-crooked.com/a/e1376beff0c157a1

class Base{
private:
    virtual void do_run() = 0;
public:
    void run(){
        do_run();
    }
};

class A : public Base {
public:
    // uplift ??
    virtual void do_run() override {}
};


int main()
{
    A a;
    a.do_run();
}

Why can I override a PRIVATE virtual method as public?

13
  • 6
    virtual and override simply do not pay any attention to access modifiers. Notably, it's not part of the function signature, which is what virtual and override look at. Commented Jun 6, 2018 at 14:26
  • 10
    @AlexG But they can, that's the point of this question. Commented Jun 6, 2018 at 14:27
  • 3
    @tower120: UKMonkey means that Base& base = a; base.do_run(); would produce error.
    – Jarod42
    Commented Jun 6, 2018 at 14:29
  • 6
    THis is a very good question. IMO this is an error in the language design contradicting "private members cannot be seen by subclasses".
    – sebrockm
    Commented Jun 6, 2018 at 14:30
  • 2
    @sebrockm -- "private members cannot be seen by subclasses" is simply wrong. They cannot be accessed; their names are visible. Commented Jun 7, 2018 at 18:55

5 Answers 5

23

According to https://en.cppreference.com/w/cpp/language/virtual#In_detail overriding a base's virtual member function only care about the function name, parameters, const/volatile-ness and ref qualifier. It doesn't care about return type, access modifier or other things you might expect it to care about.

The linked reference also specifically notes that :

Base::vf does not need to be visible (can be declared private, or inherited using private inheritance) to be overridden.

Nothing that I can find explicitly gives permission to do this, but the rules of overriding do not prevent it. It's allowed by virtue of virtual functions and function overriding existing and not disallowing this case.

If you are asking why this is how the language is, you may have to ask the standardization committee.

18
  • 1
    @tower120 I don't understand what you are asking. That example works because private member functions can be overridden. Commented Jun 6, 2018 at 14:40
  • 3
    If you accidentally override the as public, you've misused the pattern. I'm not sure how you can modify the pattern to mitigate that risk. But the same is true of countless features and technologies in c++. It's possible to make a mistake using it. This is one of the main reasons why bugs exist. Though in this case, making it public only exposes you to further problems, it doesn't cause any problems on it's own. Commented Jun 6, 2018 at 14:44
  • 1
    @tower120 when you declared the method as virtual and especially as pure virtual, you have to implement it. If you are asking for it, it cannot be accidental Commented Jun 6, 2018 at 14:45
  • 1
    @tower120 The point is that the derived type's override is seen as a match for the base type's virtual member, regardless of their access modifier. Nothing in the language gives explicit permission to do this, but the rules of overriding do not prevent it. It's allowed by virtue of overriding existing and not disallowing this case. It sounds like you're already convinced that this isn't allowed and are looking for someone to validate that all of the documentation is wrong. Note that you aren't changing any access modifiers. You're supplying a new function with different a different access. Commented Jun 6, 2018 at 14:56
  • 2
    @FrançoisAndrieux Your avatar is a strangely good match for this answer. Well, given your activity in C++ tag, maybe not only this one...
    – Frax
    Commented Jun 6, 2018 at 21:20
12

That behavior is intended. If a method is virtual then it's meant to be customizable by derived classes, regardless of access modifier.

See here

1
  • 3
    It kind of breaks the private-ness of private inheritance.
    – curiousguy
    Commented Jun 12, 2018 at 23:11
4

Why I can override PRIVATE virtual method as public???

Because you look at the base method being private at wrong angle. B::do_run being private means "only members and friends of this class can use it". To prohibit derived classes from overriding it we would need separate specifier but we can simply make it not virtual. Class A on another side allows anybody to call A::do_run() and it is up to class A designer to decide so. So there is no uplift as you see it.

5
  • Well, there is an uplift: A is neither member nor friend, so I would expect it to have no access to private function of B whatsoever. This is incredibly inconsistent.
    – Frax
    Commented Jun 6, 2018 at 21:03
  • 2
    @Frax A has no access to a private function of Base. It cannot place a call to Base::do_run. It can customize a customization point that Base offered, but that doesn't involve any form of access.
    – Cubbi
    Commented Jun 6, 2018 at 23:52
  • @Cubbi The validity of the declaration overriding the base class declaration is checked by comparing to the base class, inaccessible, declaration. Saying that it isn't an "access" really is a stretch.
    – curiousguy
    Commented Jun 30, 2018 at 18:20
  • @curiousguy Are you inventing a new meaning for the word "access"? Its technical meaning (to which "private:" relates) applies when compiling a function call.
    – Cubbi
    Commented Jul 1, 2018 at 15:13
  • @Cubbi I wasn't aware of a technical meaning different from the common sense meaning. Your code depends on a declaration. Compilation must access that declaration. Common sense. That's all.
    – curiousguy
    Commented Jul 1, 2018 at 18:01
2

Notice that this implementation does not change the way how the base class is accessed and a construct:

Base& b = a;
b.do_run();

will not work.

I remember there is some rationale behind it described in more detail in "Effective C++" by Scott Meyers. But the key practical feature is to be able to use such flexibility in the opposite direction, to override public base class members with private functions in a derived class forcing the client to use the base class as the interface and not be tempted to use directly the derived one that should remain a hidden implementation.

3
  • Yes, overriding, implementing, but not changing visibility.
    – tower120
    Commented Jun 6, 2018 at 15:49
  • @tower120 You may change the visibility of members in a derived class but regardless of this the base class interface will always work following its own definition. Whatever happens in a derived (implementing) class is expected to be an implementation detail and be less visible and not affecting the base interface. And with providing implementation some extra flexibility may be sometimes handy. Commented Jun 6, 2018 at 16:01
  • @jszpilewski Access control (public, protected, private) is not the same as visibility.
    – curiousguy
    Commented Jun 30, 2018 at 18:18
0

If the intention is to write a private code for the base-class & to prevent the possibility of override it, implement the private function in the base class, and declare it final, otherwise: When should someone use private virtuals? ISOCPP.ORG FAQ

5
  • If the function should not be overridden, just don't make it virtual. (Am I missing something?)
    – curiousguy
    Commented Jun 30, 2018 at 4:32
  • Let's say you have a base-class B with (no virtual) void f1(int); and a derive-class D with void f1(int); - bad (hope for a warning): D::f1() hides B::f1(). Visual Studio C++ Compiler warnings C26434 on Code-Analysis. On the other hand: if you have base-class B with virtual void f1(int) final; and derived-class D with void f1(int); - you get an ERROR: overriding final function.
    – Amit G.
    Commented Jun 30, 2018 at 19:44
  • I see but it the effort worth it? Also, making a class polymorphic as a (small) cost.
    – curiousguy
    Commented Jun 30, 2018 at 19:46
  • Overall, name hiding is dangerous and error-prone. In some cases, as a strong design-statement, I assume it worth the "effort".
    – Amit G.
    Commented Jun 30, 2018 at 19:59
  • But compilers often can warn about name hiding without forcing the programmer to make member function virtual final. Also the method can only work for non static member functions, so this is no fix for cases of hiding of a static member, a data member, a type definition member, or a non static template function.
    – curiousguy
    Commented Jun 30, 2018 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.