Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:06:53+00:00 2026-06-09T03:06:53+00:00

The question is from C++ faq. http://www.parashift.com/c++-faq-lite/protected-virtuals.html Code using public overloaded virtuals: class Base

  • 0

The question is from C++ faq.

http://www.parashift.com/c++-faq-lite/protected-virtuals.html

Code using public overloaded virtuals:

class Base {
public:
  virtual void f(int x);    ← may or may not be pure virtual
  virtual void f(double x); ← may or may not be pure virtual
};

Improving this via the Public Overloaded Non-Virtuals Call Protected Non-Overloaded Virtuals idiom:

class Base {
public:
  void f(int x)    { f_int(x); }  ← non-virtual
  void f(double x) { f_dbl(x); }  ← non-virtual
protected:
  virtual void f_int(int);
  virtual void f_dbl(double);
};

The author said:

The idea of the Public Overloaded Non-Virtuals Call Protected Non-Overloaded Virtuals idiom is to change the public overloaded methods to non-virtuals, and make those call protected non-overloaded virtuals.

But I do not understand what the author said on how this idiom improve risk:

the idiom packs the complexity of properly managing the hiding rule into the base class (singular). This means the derived classes (plural) more-or-less automatically handle the hiding rule, so the various developers who produce those derived classes can remain almost completely focused on the details of the derived classes themselves — they need not concern themselves with the (subtle and often misunderstood) hiding rule. This greatly reduces the chance that the writers of the derived classes will screw up the hiding-rule.

Why does this solve the hiding problem? From what I understand, name hiding has nothing to do whether the member function is ‘virtual’ or not. If a derived class of ‘base’ rewrites a function f(), it will still hide the f(int) and f(double), right?

From this idiom all I can see is that author change the ‘base’ virtual f() to non virtual, and put helper functions f_int(), f_dbl() in ‘protected virtual’, like the name of idiom said. It does nothing good yet but in contrary eliminates possibility of dynamic binding from base class pointer/reference. What is the true benefit of this idiom?

Update

Kerrek, are you saying this? I do not fully understand the second paragraph of your answer. Could you give an example?

class base {
public: 
    virtual void f(int x);
    virtual void f(double x);
}

class derived : public base {
public:
    virtual int f(int x); // oops, will hide base::f(int x) AND base::f(double x)
}

base *bp = new base();
base *dp = new derived();
bp->f(int i);    // ok
dp->f(int i);    // surprise!
dp->f(double d); // compile error!


class Base {
public:
    void f(int x)    { f_int(x); }  
    void f(double x) { f_dbl(x); } 
protected:
    virtual void f_int(int);
    virtual void f_dbl(double);
};

class derived : public base {
public:
    // nothing to override here 'cause f() is non virtual
protected:
    // because f_int() and f_dbl are unique names, override or hide f_int() will not affect f_dbl()?
    virtual int f_int(int);  // oops, will hide base::f(int x), but developer may want this on purpose
                             // no effect on f_dbl(), which is good
}

base bobj;
derived dobj;
bobj.f(int i);    // ok
dobj.f(double d); // ok
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:06:54+00:00Added an answer on June 9, 2026 at 3:06 am

    If a derived class declares void f(int), then it overrides the virtual function, and the virtual specifier is implied. If a derived class declares int f(int), it hides the base function. I gather that you’re familiar with this.

    The problem comes when you want others to develop code based on your base class. With the naive approach, each derived class must be careful to add the correct override so as not to accidentally hide the function and get a working, but wrong program (i.e. the user says f() but gets the wrong thing). With the “public non-virtual” idiom, the user always calls f() in confidence, and the library developer can override only those parts that she’s interested in by overriding a uniquely named, protected function, without having to touch a name that may affect other users.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question based on this question In the section http://www.parashift.com/c%2B%2B-faq-lite/private-inheritance.html#faq-24.3 the following
From: http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.9 Three keys: ROI, ROI and ROI. Every interface you build has a
http://www.parashift.com/c%2B%2B-faq/cpp-objs-passed-to-c.html The answer is in two parts. First part I guess is till the
Question from beginner. Why code <%= System.DateTime.Now.ToLongDateString() %> is not executed in ASP.NET. Could
Question from the one interview. Please explain what does this C++ code mean: void
In my opinion, the following code (from some C++ question) should lead to UB,
I am using the following code for display questions and answers from admin. <?php
I am coming back from after reading this c-faq question I am totaly confused
If the following from the C++ FAQ Lite is true: a function name decays
Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.