Suppose I have a class with an two inline functions:
class Class {
public:
void numberFunc();
int getNumber() { return number; }
private:
int number;
};
inline void Class::numberFunc()
{
number = 1937;
}
I instantiate that class and I call both of the 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 it is also my understanding that the code within the body of an inline function is just inserted in place of where it was called. It seems that, as a result of that insert, I should not be able to directly access the member variable number because, as far as I know, the code in main() to the compiler would look like:
main() {
Class cls;
cls.number = 1937;
cout << cls.number << endl;
return 0;
}
Can someone explain to me why I am still able to access those private members, or correct me on my understanding of inline functions? I know that compilers have the option to ignore the inline on some functions; is that what is happening here?
Output:
1937
The rules for accessing private members of a class are enforced by the compiler on your C++ code. These rules don’t apply directly to the output of the compiler, which is the code that a computer exectues.