This is another question about inlining a function.
But I will take possible comments and answers right away:
- Defining a function inside a class makes it inline automatically.
- The same behaviour can be achieved by marking a function with inline outside of the class.
- An inline function doesn't have to be inlined. It is completly up to the compiler to inline it.
Now, my question:
When inlining a function means to copy the body to the place where it is being called.
Isn't it right to assume that the compiler will not inline it if it accesses private or protected members?
The programm would literally not be able to access the members right?
I want to know that because to me it must look quite weird if someone inlines a function that obviously cannot be inlined.
Here is an example:
//Declaration
class Controller
{
public:
bool bHasTarget();
private:
const Object* pTarget;
};
//Definition
inline bool Controller::bHasTarget(){
return !(pTarget == nullptr); //<- This cannot be inlined...Can it?
}
The Compiler can Access everything. The restrictions are only valid for the programmer. This means there are no restrictions for the Compiler to Access any variables! At the end every variable is just translated to an address which can be accessed. So for the Compiler it is no Problem to inline the code you provided!
There are also some "cheats" to Access private variables as a programmer. e.g