1) This is can be used as an accessor; you are right. If you need to modify the field of the already constructed class instance, what else can we do? We can use this accessor or side effect from some (non-const) instance (non-static) function called on this instance.
2) This form of writing constructor is not preferred, preferred on is (3)
3) This is the preferred form of field initialization in the constructor. It has nothing to do with inheritance at all as it can be used on the type completely isolated from any inheritance. However, you can use it with inheritance. Remember two things: automatic call of the base class constructor and one important warning: you should not call virtual functions from a constructor. The explanation of this is pretty complex; if I find the article on this topic, I'll add the reference for you.
I guess, you confusion is all about why (3) is needed; and now you may want to know why I call (3) a preferred way? The short answer is: this is due to the C++ ideology. More exactly, this is based on the fundamental difference between initialization of the object and assignment operation. Is assignment is not possible, the initialization always takes place. I think this special syntax for initialization by constructor is used to strictly isolate "just initialization" from any other code which can cause side effects. The form (3) can only be used for initialization, which reduces the risk of some bugs based on the difference between initialization and assignment.
The form (3) is basically preferred, but not in all cases.
AspDotNetDev recently offered me a very simple example where the form (2) is much better, please see http://www.codeproject.com/Messages/3849028/Code-Sharing-Cplusplus-Intitialization-List-Expens.aspx
There is no essential difference between setting up of the class instance fields by value, by reference or "by pointer". You rather need to read about different ways of passing parameters. Additionally, pay attention on how the
modifier is used and treated. By the way, "by pointer" in not a separate mechanism of passing parameters. It is actually passing some parameter by value, but the parameter itself can be of pointer type or not. However, these two cases create identical binary code: a) you pass a variable of the type
by reference, b) you pass a variable of the type
by value. Despite identical binary semantics, the syntax is different for both the caller and the function implementation. I would say passing
is more the legacy of C style; passing by reference (
) is often preferable (but not always possible).
Here is one example when one may prefer by reference: with pointer-by-value approach one can pass null pointer. It can be prevented using the by-reference.
You also need to read about C++ references per se. And
. Don't mix reference type notation with "address" operator "&".
Sorry, I can't see any sense in you last question related to two last line of your code about
. As you did not declare
, its even harder to guess what did you mean to achieve or understand. I guess you can understand it if you read about C++ references.