5

Is there any valid reason for not using public virtual methods?

I have read somewhere that we should avoid using public virtual methods, but I want to confirm from experts if this is valid statement?

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
  • Since a derivation can usurp the access restriction of a virtual method simply by overriding it, I'm genuinely curious what the author of that statement has in mind in making that claim. – WhozCraig Mar 24 '14 at 06:48
  • There is, and that is when you don't need them. – Mark Garcia Mar 24 '14 at 07:21

3 Answers3

8

For good and stable API design, the Non-Virtual-Interface is a good idiom.

I'm going to defer to the existing good literature on this:

See also these splendid answers:

(Sumant Tambe has an intriguing matrix of Design Intent Implications on his blog, which has a few more bits on design intent.)

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
6

Making virtual functions non-public allows the base class to create a protocol around them. If nothing else, this could be used for some accounting/profiling requiring instrumentation of only the base class. The base class public interface could be inline functions merely forwarding to the virtual functions. Sadly the restrictions can be relaxed in derived classes, i.e., a derived class may give public access to a virtual function from the base class.

There is actually another important reason for making virtual functions protected: when overloading virtual functions (e.g., the do_put() members in std::num_put<...>) it is easy to accidentally hide other overloads when overriding just one of the functions. When the virtual functions is both the customization point as well as the call interface, this can easily lead to surprising behavior. When the virtual functions are protected it is clear that the call interface and the customization interfaces are actually different and the problem is avoided even when using the derived class directly. The virtual functions probably want to be protected to allow overriding functions to call the default implementation from the base class. If there is no default implementation, the virtual function can also be private.

This answer discusses why virtual functions shouldn't be public. Whether you should have virtual functions in the first place is a separate question with a somewhat non-trivial answer.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • +1 for actually taking the time to explain on the spot :). Oh **and** +1 for that last paragraph. well worth the read. – sehe Mar 24 '14 at 07:57
1

One thing comes to my mind - not using public virtual methods simplifies separating class's interface from implementation. Say, you provide a public method doing something:

public:
    virtual void DoSth()
    {
    }

After some time a change is introduced, which requires initializing and finalizing the base class before doing something. If you already derived from your class a few classes, you'll have to change their implementations. But if you've written it in the following way:

protected:
    virtual void InternalDoSth()
    {
    }

public:
    void DoSth()
    {
        InternalDoSth();
    }

It will be only a matter of changing DoSth's implementation:

public:
    void DoSth()
    {
        InitializeSth();
        InternalDoSth();
        FinalizeSth();
    }

Creating two-level virtual functions is very cheap and provides you with an additional layer, which allows you easily controlling the way virtual method is called in the future.

Spook
  • 25,318
  • 18
  • 90
  • 167