While I would assume that in VC++ this would be a no brainer, it’s still worth asking.
When creating a getter method for a class that only returns the value of a protected/private member, does the compiler optimize this call so it’s the equivalent of referencing that member without having to friend the class and without the overhead of a full method call?
Yes. Both variants compile to the same thing:
Note that it’s not as simple as always replacing
t.get()witht.x, for the compiler. Consider something like this:This shouldn’t compile, because the result of the function call is an rvalue and
+=(for primitives) requires an lvalue. The compiler will check for things like that.