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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:58:20+00:00 2026-06-14T01:58:20+00:00

Now I have been learning about inline functions and I encountered something that really

  • 0

Now I have been learning about inline functions and I encountered something that really made me confused

See this class

class Nebla{
private:
    int x;
public:
    inline void set(int y){x=y;}
    inline void print(){cout<<x<<endl;}
};

it has a private data member: int x;

And it has two public inline functions: set(int y) and print()

Now since they two functions are inline, when they are called the compiler replaces the function call with the contents of the function.

So if I do this

Nebla n;
n.set(1);
n.print();

since the two functions are inline, It should be the equivalent of this:

Nebla n;
n.x=1;
cout<<n.x<<endl;

but wait a second, x is private. Therefore, this shouldn’t work.

But it does and I’m confused why it does work although normally you cant access private members from outside the class?

Can anyone explain to be why you can access private data members from outside the class but when a member function is inline it can although inline just replaces the function call with the contents of the function?

  • 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-14T01:58:21+00:00Added an answer on June 14, 2026 at 1:58 am

    Data member protection is purely conceptual. It exists only at the compiler level. It is checked and enforced when the compiler translates the source code. Once the code is compiled, there’s no difference between public and private data members anymore, i.e. there are no physical mechanisms that would enforce access control and prevent access to private data members.

    Member access is enforced by the compiler in accordance with the language specification. The language specification states that class member functions (regardless of whether they are inline or not) have access to private members of the class. So the compiler allows that access. Meanwhile, other functions are prohibited such access, so the compiler complains about it.

    In your example you are accessing private data member from a member function. That is allowed, so the code compiles, i.e. the compiler does not complain. What happens later in the generated machine code, after the function gets inlined, is completely irrelevant. That’s all there is to it.

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

Sidebar

Related Questions

I've been learning wpf for about a week now.. and i have a basic
I have been learning python for some time now. While starting this learning python
I'm learning scheme and until now have been using guile. I'm really just learning
I have been learning about various functional languages for some time now including Haskell,
I have been learning about how to set up Tomcat's connection pool through this
I've been learning Android for about two weeks now (expert in as2/3). I have
I have been neglecting learning about caching for quite some time now, and although
I have been learning opengl for about 4-5 months now. I am ready to
I have been reading this ebook about DDD and it says that only highly
I have been learning Python2.7 for a little bit now. I'm using Windows 7

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.