wrote:
> > class CustomType
> > {
> > public:
> > CustomType(){_i = 0;}
Use Constructor initialization list
CustomType() : _i(0) { } ;
> > CustomType(int i) : _i(i) {}
> > private:
> > int _i;
> > };
> >
> > class MyClass
> > {
> > public:
> > MyClass() : _member(4){}
> > private:
> > CustomType _member;
> > };
> >
Donot use names starting with an underscore - they are reserved for
implementation.
> > Will _member be ever created before the initialization list in MyClass?
> > Am I guarantueed that it will only get created that once?
the initialization list will initialize the member "_member" when
constructor of the class CustomType is called. Space for _member is
reserved when you instantiate the class. This is immediately followed
by the constructor call to initialize the object. So , though the
object creation and initialization take place at two distinct points in
time, as far as the programmer is concerned, they are "atomic".
Also, if a member is of a user-defined type and it is not initialized
in the constructor initialization list, then the compiler calls the
default constructor for that member before executing the code of the
constructor.
>
> A further question: Imagine CustomType is an ABC. There's no way I can
> initialize that in the initialization list, is there? I'd actually have
> to use a pointer in this case, and assign it a value from making a new
> concrete class down the inheritance chain, right?
If CustomType is an absract base class, you cannot have an instance of
CustomType as a member of MyClass. So you are right in pointing out
that you would actually need a pointer there.
HTH.