1

considering below example

#include <iostream>
#include <string>

class A
{
public:
    virtual void foo() { std::cout<< "FOO A\n"; }

private:
    void bar() { std::cout<< "BAR A\n"; }
    virtual void vbar() { std::cout<< "VBAR A\n"; }
};

class B : public A
{
public:
    void foo() { std::cout<< "FOO B\n"; bar(); vbar(); }
private:
    void bar() { std::cout<< "BAR B\n"; }
    virtual void vbar() { std::cout<< "VBAR B\n"; }
};

int main()
{
    A* b = new B();
    b->foo();
}

The output will give us

FOO B
BAR B
VBAR B

Since its simple example first that come to my mind I cant figure out any private virtual method use case. In case of public virtual method, the base pointer class interface will adapt to its defined vtable, but as in given example for private virtuals it doesnt matter

Piodo
  • 616
  • 4
  • 20
  • 3
    Does this answer your question? [Private virtual method in C++](https://stackoverflow.com/questions/2170688/private-virtual-method-in-c) – darcamo May 20 '20 at 22:09

2 Answers2

1

One possible use is for letting a base class define a structure, and having derived classes implement the behaivour of the components of said structure (the template method pattern). For example,

struct foo
{
    void do_stuff() {
        // defines order in which some operations are executed
        do_op1();
        do_op1();
        do_op3();
    }
private:
     // These don't have to be pure virtual. A base,
     // default implementation could also be provided.
    virtual void do_op1() = 0;
    virtual void do_op2() = 0;
    virtual void do_op3() = 0;
};

// implements the operations
struct foo1 : foo
{
private:
    void do_op1() override { ... }
    void do_op2() override { ... }
    void do_op3() override { ... }
};

The virtual methods are private because it does not make sense to call them in isolation. The base class knows when and how to call them.

There are probably simpler and better ways of implementing this in "modern C++", but this kind of thing might have been seen in the 90s and 00s.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

There are situations where it might be useful, some argue, that it should be the prefered method, when possible, like Herb Sutter:

Guideline #2: Prefer to make virtual functions private.

...This lets the derived classes override the function to customize the behavior as needed, without further exposing the virtual functions directly by making them callable by derived classes (as would be possible if the functions were just protected). The point is that virtual functions exist to allow customization; unless they also need to be invoked directly from within derived classes' code, there's no need to ever make them anything but private. But sometimes we do need to invoke the base versions of virtual functions (see the article "Virtually Yours"[5] for an example), and in that case only it makes sense to make those virtual functions protected, thus:

Guideline #3: Only if derived classes need to invoke the base implementation of a virtual function, make the virtual function protected...

http://www.gotw.ca/publications/mill18.htm

Community
  • 1
  • 1
anastaciu
  • 23,467
  • 7
  • 28
  • 53