KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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?
Tags (comma-separated)
Save Edits
Cancel