Why do inline functions allow you to manipulate private member variables?

Suppose I have a class with two built-in functions:

class Class {
public:
   void numberFunc();
   int getNumber() { return number; }
private:
   int number;
};

inline void Class::numberFunc()
{
   number = 1937;
}

I create an instance of this class, and I call both functions in the class:

int main() {
   Class cls;
   cls.numberFunc();
   cout << cls.getNumber() << endl;
   return 0;
}

I understand that both inline functions are still members of the class, but also I understand that the code inside the body of the inline function is simply inserted instead of where it was called. It seems that as a result of this insertion I should not have direct access to the member variable number, because, as far as I know, the code in the main()compiler will look like this:

main() {
   Class cls;
   cls.number = 1937;
   cout << cls.number << endl;
   return 0;
}

- , ? , inline ; , ?

:

1937

+5
3

++-. , , .

+9

inline , , , . . , , . ++.

- -. .

, ( , ). , .

+6

An access privatespecifier is a restriction for class users (programmers), not the compiler. The compiler can do whatever it wants, as long as the observed behavior of the program is the same.

+4
source

All Articles