5.4 Parametrized component constructor

Let us build on top of the previous section. What if we have the following?

class SomeValues
{
    Value va, vb, vc;
  public:
    SomeValues(int a, int b, int c);
};

We want to use the parametrized constructor of SomeValues to initialize its components. Recall that the constructor subroutines of components are inovked automatically. This means va, vb and vc are initialized by their constructor with no parameters, which essentially set them all to 0.

However, there is a method to invoke the parametrized constructors of data members:

SomeValues::SomeValues(int a, int b, int c):
  va(a), vb(b), vc(c)
{
  // additional initializaation code
};

This constructor of SomeValues explicitly calls the constructors of components va, vb and vc. The compiler understands the explicit calls to initialize va, vb and vc. Consequently, the default (without parameters) constructors of the components are not called.



Copyright © 2006-09-26 by Tak Auyeung