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 724865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:16:20+00:00 2026-05-14T06:16:20+00:00

I’m confused about the errors generated by the following code . In Derived::doStuff, I

  • 0

I’m confused about the errors generated by the following code.
In Derived::doStuff, I can access Base::output directly by calling it.

Why can’t I create a pointer to output() in the same context that I can call output()?

(I thought protected / private governed whether you could use a name in a specific context, but apparently that is incomplete?)

Is my fix of writing callback(this, &Derived::output); instead of callback(this, Base::output) the correct solution?

#include <iostream>
using std::cout; using std::endl;

template <typename T, typename U>
void callback(T obj, U func)
{
  ((obj)->*(func))();
}

class Base
{
protected:
  void output() { cout << "Base::output" << endl; }
};

class Derived : public Base
{
public:
  void doStuff()
  {
// call it directly:
    output();
    Base::output();

// create a pointer to it:
//    void (Base::*basePointer)() = &Base::output;
// error: 'void Base::output()' is protected within this context
    void (Derived::*derivedPointer)() = &Derived::output;

// call a function passing the pointer:
//    callback(this, &Base::output);
// error: 'void Base::output()' is protected within this context
    callback(this, &Derived::output);
  }
};

int main()
{
  Derived d;
  d.doStuff();
}

Edit: I’d love to know where this is in the stardard, but mostly I’m just trying to wrap my head around the concept. I think my problem is that callback doesn’t have access to protected members of Derived, but it is able to call Derived::output if you pass it a pointer. How is a protected member of Derived that comes from Derived different from a protected member of Derived that comes from Base?

  • 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-14T06:16:20+00:00Added an answer on May 14, 2026 at 6:16 am

    In short, it’s “because the standard says so.” Why? I don’t know, I’ve emailed a couple of the standards guys, but haven’t received a response, yet.

    Specifically, 11.5.1 (from C++0x FCD):

    An additional access check beyond
    those described earlier in Clause 11
    is applied when a non-static data
    member or non-static member function
    is a protected member of its naming
    class (11.2)114 As described earlier,
    access to a protected member is
    granted because the reference occurs
    in a friend or member of some class C.
    If the access is to form a pointer to
    member (5.3.1), the
    nested-name-specifier shall denote C
    or a class derived from C. All other
    accesses involve a (possibly implicit)
    object expression (5.2.5). In this
    case, the class of the object
    expression shall be C or a class
    derived from C.

    Edit:

    Also, you’ll see that you change the code to the following, according to what the standard specifies, it will compile (and run) cleanly:

    void (Base::*derivedPointer)() = &Derived::output;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since Python 2.5 you can use max with a key… May 14, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer you need to create images for those states like focussed… May 14, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer Following a suggestion from Bruce Eckel's book Thinking in Java,… May 14, 2026 at 4:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.