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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:38:57+00:00 2026-05-23T01:38:57+00:00

I have a class that looks something like this: class ClassA { public: float

  • 0

I have a class that looks something like this:

class ClassA
{
  public:
    float Get(int num) const;
  protected:
    float& Get(int num);
}

Outside of the class, I call the Get() function.

float foo = classAInstance.Get(i);

I expect this to call the public version, but instead Visual Studio errors out:

error C2248: 'ClassA::Get' : cannot access protected member declared in class 'ClassA'

When commenting out the protected overload and removing all references to it, the code compiles.

Why does the compiler try to use the inaccessible member when an accessible one is available? Is there an accepted way to force the compiler to choose the correct overload? Is there a reference to the resolution rules for member functions somewhere?

  • 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-23T01:38:57+00:00Added an answer on May 23, 2026 at 1:38 am

    It’s true, overload resolution takes place before accessibility checks. Section 13.3 of the standard ([over.match]) says:

    Overload resolution is a mechanism for selecting the best function to call given a list of expressions that are
    to be the arguments of the call and a set of candidate functions that can be called based on the context of
    the call. The selection criteria for the best function are the number of arguments, how well the arguments
    match the parameter-type-list of the candidate function, how well (for non-static member functions) the
    object matches the implicit object parameter, and certain other properties of the candidate function. [ Note:
    The function selected by overload resolution is not guaranteed to be appropriate for the context. Other
    restrictions, such as the accessibility of the function, can make its use in the calling context ill-formed.
    — end note ]

    The usual fix is to give the public and protected functions different names.


    Note, this is useful sometimes, example:

    class Blah
    {
        const std::string& name_ref;
    
        Blah(const char*) = delete;
    
    public:
        Blah(const std::string& name) : name_ref(name) {}
    
        void do_something_with_name_ref() const;
    };
    
    std::string s = "Blam";
    Blah b(s); // ok
    

    Note that name_ref will only be read from, so it’s appropriate to make it const. However, const references can bind to temporaries, and binding name_ref to a temporary would be a dangling reference, resulting in undefined behavior in do_something_with_name_ref().

    Blah c("Kablooey!"); // would be undefined behavior
                         // the constructor overload makes this a compile error
    

    The private constructor overload prevents a temporary std::string from being implicitly constructed and bound.

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

Sidebar

Related Questions

Say I have a class that looks something like this: class SomeClass { int
I have Servlet that looks something like this: public class MyServlet extends Servlet {
I have an object that looks something like this: public class Student { public
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class that looks like this: public class TextField : TextBox {
I have a base Color class that looks something like this. The class is
I have a C++ class hierarchy that looks something like this: class A; class
I have a class that roughly looks like this: public class ViewModel { public
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)

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.