The goal of what I am trying to do is to give some variables of type Z
and Y scope of class X but delay there initalization until all other
dependencies have been met. For example the Constructor of X may have
to go to the database before it can populate Z and Y with thier values.
So in that case I don't think constructor initalizer list technique
would be able to be used in that case.
And why not? Whats preventing from initializing a variable to a valid
state and then reset the variable with a some dependancy when
available? Likewise, whats preventing you from completing some
dependancy and still intialize the variables in that init list?
Remember job #1, always , always initialize all your members. Even if
you will never use them. Take as an example a pointer. If you set that
pointer to 0 in the init list and happen to use it without
initialization it by accident - the result and the solution will be
obvious. If that pointer isn't initialized at all, you'll dig high and
low for the bug for a week. The same applies for any member.
Why take away the support a compiler can give you?
Interestingly enough, take a look at what happens if you do have a
dependancy:
#include <iostream>
class Z
{
char m_char;
public:
Z() : m_char( getInput() ) { std::cout << "Z()\n"; } // def ctor
~Z() { std::cout << "~Z()\n"; }
/* member functions */
char getInput() const
{
std::cout << "enter a char between b and e (default: a): ";
char c;
std::cin >> c;
if( (c >= 'b' && c <= 'e' ) && std::cin.good() )
{
return c;
} else {
return 'a';
}
}
const char& get() const { return m_char; }
};
int main()
{
Z instance;
std::cout << "instance.m_char = ";
std::cout << instance.get() << std::endl;
return 0;
}
/* sample output
enter a character between b and e (default: a): c
Z()
instance.m_char = c
~Z()
*/
Try a primitive array of Zs.