Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are inline functions allowed to manipulate private member variables?

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

like image 866
Austin Moore Avatar asked Sep 24 '12 01:09

Austin Moore


People also ask

What is the purpose of inline function?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

What is inline function What are their advantages give an example?

Inline functions provide following advantages: 1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function.

What is an advantage of using inline?

Advantages of using Inline FunctionsNo function call overhead occurs; hence enhanced program speed is obtained. It helps in saving the overhead of return call from a function. Helpful while calling a function in saving the overhead of variables push/pop on the stack.

What are the advantages of inline function over normal function?

Advantages of Inline Function in C++The compilation speed of the program gets increased since the inline functions prevent function call overhead. Inline functions that are small in length can be used in embedded systems as well since these functions yield lesser code than the function call and return.


2 Answers

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.

like image 109
Code-Apprentice Avatar answered Sep 30 '22 19:09

Code-Apprentice


The inline keyword does mean that programmer thinks that compiler may if it so wants to insert the code at place of call. Compiler may inline other functions too without the keyword. Compiler may think that programmer is fool and ignore the keyword and not inline. It is all by C++ standard.

The inline member function is otherwise quite normal member function. No other privileges or restrictions.

Inlines do not cause errors that the function is defined by multiple compilation units (that include the header file where the inline function is defined). That may be one reason why people write inline functions.

like image 26
Öö Tiib Avatar answered Sep 30 '22 17:09

Öö Tiib