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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:15:20+00:00 2026-05-13T07:15:20+00:00

I have two questions. In C++, a static member function has direct access to

  • 0

I have two questions.

In C++, a static member function has direct access to a public non-static data member defined in the same class?
False

In C++, a non-static member function has direct access to a private static data member defined in the same class?
True

My note say false for the first question and true for the second one. I just cannot find out why? Can you explain why this is? Thank you.

P.S. I’m studying for my final and I cannot seem to figure out why.

  • 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-13T07:15:21+00:00Added an answer on May 13, 2026 at 7:15 am

    Everyone’s in agreement, but should be very careful about their wording, because actually static member functions do have access to public non-static data members. For that matter, they have access to private non-static data members too. They just need an object to operate on, to access its members. This could be a parameter, or a global, or created in the static member function, or acquired via one of those things.

    The following code is fine:

    class foo {
    public:
        int a;
        // static member function "get_a" ...
        static int get_a(foo *f) {
            // ... accesses public non-static data member "a"
            return f->a;
        }
    };
    

    So we ask ourselves, what’s the difference between “access” and “direct access”?

    I guess what’s meant by “direct access” here must be “using only the name of the data member, without specifying an object”. Everyone always needs to have an object in order to access non-static members – that’s what non-static means. Non-static member functions just don’t have to mention which object if they don’t want to, because this is implicit. Hence their access to non-static data members can be direct.

    The reason non-static member functions have direct access to private static data members is firstly that the code is in a member of the class, hence it can access private data members. Second, you never need an object in order to access static data members (you can specify one if you want, but all that’s used is the static type of the expression, not the actual object), hence the access is direct.

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

Sidebar

Related Questions

I have two classes : import android.cla; public class CW { public static void
I have two questions. I know it is possible to declare class objects in
I have two questions about static + const members and static classes. One: Why
Why can't I initialize non-const static member or static array in a class? class
I have two questions, stemming from observed behavior of C# static methods (which I
I have two questions: First: I have a function which returns a HashMap. To
These are two questions about static locals that have always bothered me and I
I have two questions here. How can I show the header of the pop
I have two questions. How can I get the spinner in the figure below
I have two questions. Does it make sense when I have to choice of

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.