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

  • SEARCH
  • Home
  • 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 7604087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:49:11+00:00 2026-05-30T23:49:11+00:00

From Gang of Four on the Template Method Pattern: Three implementation issues are worth

  • 0

From Gang of Four on the Template Method Pattern:

Three implementation issues are worth noting:

  1. Using C++ access control. In C++, the primitive operations that a template method calls can be declared protected members. This ensures
    that they are only called by the template method. Primitive operations
    that must be overridden are declared pure virtual. The template method
    itself should not be overridden; therefore you can make the template
    method a nonvirtual member function.

“This ensures that they are only called by the template method.” is not true, is it? As the primitive methods (if some are virtual rather than pure virtual for example) could also be called from a derived class. Is it not true that only declaring the primitive methods private ensures they are only called by the template method? The private virtual primitive methods can then still be implemented (or reimplemented) in subclasses to provide the specialised behaviour which is required within the algorithm defined in the template method in the superclass.

See “Virtuality” from Herb Sutter:

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

Where he states that:

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

I don’t see any requirement within the GoF Template Method pattern for derived classes to invoke base class implementations of virtual functions, so why do Gang of Four recommend making these functions protected rather than private?

  • 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-05-30T23:49:12+00:00Added an answer on May 30, 2026 at 11:49 pm

    The answer is pretty simple:

    Design Patterns dates from 1994. At this time C++ was still evolving heavily into the language that we know today – the previous standard was finalised in 1998! Compilers were buggy and didn’t support important features that we now take for granted.

    But more importantly, many things hadn’t been discovered (in particular by mainstream programmers) yet. Take template metaprogramming, the whole of which was only “stumbled upon” around the same time.

    I highly suspect that the same is true for private virtual functions: The GoF simply weren’t aware that this was legal C++. Or if it was, it hadn’t established itself as a convention.

    In fact, most C++ code that was considered “good” in 1994 would be considered overly complex and error-prone today, even without taking C++11 into account.

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

Sidebar

Related Questions

From what I can gather, there are three categories: Never use GET and use
From security reasons, is it worth encrypting user emails before putting them into the
From wikipedia: // A class template to express an equality comparison interface. template<typename T>
From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate
The Gang of Four's Design Patterns uses a word processor as an example for
From this example http://www.senchafiddle.com/#dPZn0 This displays a first panel, then a second one... using
From a web developer point of view, what changes are expected in the development
From a desktop application developer point of view, is there any difference between developing
From what I've read, VS 2008 SP1 and Team Foundation Server SP1 packages are
From time to time I see an enum like the following: [Flags] public enum

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.