* dover:
Instantiation of classes is to create objects in areas of memory. As a side
effect of that object creation, the constructor is called. Three steps can
be identified in an instantiation of a class: memory allocation, object
creation, and call constructor.
Object creation (of an object of class type) is the same as successful
execution of a constructor set, so that's _one_ step when you think of a
sequence. Memory allocation is usually a side-effect of that single
conceptual act, because the main purpose of a constructor is to couple
allocation and creation so that you cannot have one without the other. To
decouple them you have to use special "unsafe" features of the language.
My understanding is the call of constructor that triggers memory allocation
and object creation, though I understand when in constructor, the memory has
been allocated and object created. Can someone straight this out for me?
You seem have two different concepts of call mixed up. One is the call in
your code, e.g.
T anObject( anArgument );
^^^^^^^^^^^^^^^^^^^^^^
or
T* pObject = new T( anArgument );
^^^^^^^^^^^^^^^
This source code call results in memory allocation + execution of the
T constructor (as well as constructors of base classes and other members,
if any). Execution of the constructor is often (ambigiously) called a
constructor call, including in the Holy Standard, but it's at a lower
level, as _one_ of the results of the source code call, and I find it much
more clear to instead refer to it as "constructor execution". When the
constructor executes the memory has been allocated.
Paraphrasing what you wrote: "My understanding is the [execution] of
constructor that triggers memory allocation and object creation, though I
understand when in constructor, the memory has been allocated and object
created." Execution of a constructor does not trigger memory allocation,
but a source code (explicit) constructor call does: except when using the
aforementioned unsafe features it results in allocation + constructor
execution, since that's the main purpose. In good code where all object
initialization happens within constructors the object can be considered to
be fully created when the constructor execution has finished successfully.
Is it correct to say that all member variables and functions in a object
become available when inside its constructor?
Execution of a constructor is a two-step process. First, initializer-list
(which involves evaluating initializer list arguments and executing other
constructors) and then body. During evaluation of initializer list
arguments you can technically access any member (§12.6.2/7), but it can be
very unsafe: all members have not been initialized yet. In the
constructor body you're a bit safer, but it's still your job, not the
compiler's, to keep track of how far initialization has progressed and at
any given point only use things that have been initialized.