Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - static class members: initializing member class objects http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   Static Class Members: Initializing Member Class Objects
   If a class has a member object which is an instance of another  class,
   any arguments required by the constructor for that member class object
   can  be  passed  via  the constructor  of  the  enclosing  class.  The
   mechanism  involves  a member initialization list which  is  a  comma-
   separated   list   of  member  names  with   arguments.   The   member
   initialization  list  is  placed  on the  first  line  of  constructor
   definition  of the enclosing class, after the closing  parentheses  of
   its  argument list, and preceded by a colon. This is best  illustrated
   by an example:

   class Inner
   {
   public:
   Inner(int) { count = val; }
   private:
   int count;
   };

   class Outer
   {
   public:
   Outer(int, int);
   private:
   int num;
   Inner obj;
   };

   Outer::Outer(int out, int in) : obj(in)
   {
   num = out;
   }
   Member initialization lists can only appear in the definition (not the
   declaration)  of a constructor. Each member class object can be  named
   only  once  in  the list. The method can also be  used  to  initialize
   members that are basic data types, thus:

   Outer::Outer(int out, int in) : obj(in), num(out)
   {}
   Member initialization must be used to initialize data members that are
   references.

   Member   initialization  is  carried  out  before  the  body  of   the
   constructor is executed. The initialization argument is not restricted
   to  a  simple identifier; it can be a complex  expression  or  another
   object of the members own class.

   Any  class  member  object which only has  constructors  that  require
   arguments must be included in the member initialization list otherwise
   the compiler will report an error.
   Memberwise Initialization  X::X(const X&)

   It  was stated above that a member class object could  be  initialized
   with  another object of the same class. This is generally true of  any
   class  object. This copy initialization of a class object is  achieved
   by  means  of a special constructor, the X::X(const  X&)  constructor.
   This constructor is created by the compiler in order to make a copy of
   a class object, although the programmer can supply an alternative. The
   default  copy constructor achieves its aim by copying each  member  of
   the  existing object into the corresponding member of the new  object.
   For the Vector example earlier this would be equivalent to:

   Vector::Vector(const Vector& vec)
   {
   size = vec.size;
   p = vec.p;
   }
   In  this case the result leaves the two pointers p and vec.p  actually
   referencing  the same array, probably not the programmer's  intention.
   In this case the programmer can provide an alternative similar to:

   Vector::Vector(const vector& vec)
   {
   size = vec.size;
   p = new int[size];
   for (int i = 0; i < size; i++)
   p[i] = vec.p[i];
   }
   The  invocation  of  an  X::X(const  X&)  constructor  occurs  in  the
   following situations:

   Explicit initialization of one class object with another:
   Vector a(10);

   Vector b = a;
   Passing of a class object as a function parameter:
   int test(int val, Vector vec);
   Vector a(10);

   int res = test(5, a);
   Return of a class object from a function:
   Vector clear(Vector vec);
   Vector a(10);

   a = clear(a);
   The passing and return of references does not cause a copy to be made,
   only pass-by-value has this effect.

   In  the  case of a copy being made of a class which has  class  member
   objects,  the  member  objects  are  first  copied  using  their   own
   respective X::X(const X&) constructors.

   Compiler generated X::X(const X&) constructors are public.

   Memberwise  initialization  is normally inadequate  for  classes  with
   pointer  type  members  or which have a  destructor  that  deallocates
   memory. In such cases the programmer should supply a copy constructor.

   Memberwise Assignment  X::operator=(const X&)

   As  well  as  the default copy constructor described  above,  the  C++
   compiler will also provide a default member function for the situation
   where one class object is being assigned to another:
   Vector a, b;

   // a is initialized at some point

   b = a;
   This  default assignment operator function is a member by member  copy
   from the object on the right hand side of the assignment into that  on
   the left hand side. Obviously this will suffer from the same drawbacks
   as  the  X::X(const  X&) constructor. The programmer  can  provide  an
   alternative  implementation of X::operator=(const X&) to  avoid  these
   problems.

   Compiler generated X::operator=(const X&) functions are public.

   The  operator=  member  function  is special  in  that  it  CANNOT  be
   inherited by a derived class and the virtual keyword cannot be applied
   to  it.  A derived class can however be passed as an argument  to  the
   base class operator= function.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson