J
Jacek Dziedzic
Hello!
Suppose I have a class Foo that defines a default c'tor
that initializes some data using an initialization list:
Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
// and so on, quite a few members
{}
and does nothing else.
Then it turns out that I need two more c'tors that would
do some extra initialization apart from the one performed
by the default c'tor, like
Foo::Foo(string filename) {
// do something with filename
// * now wants to initialize the members in the same
// way Foo::Foo() does
}
Foo::Foo(int n) {
// do something with n
// * now wants to initialize the members in the same
// way Foo::Foo() does
}
As I can't call the default c'tor from the other c'tors,
except for a base-class c'tor, I am faced with a choice
of either repeating the initialization lists in all c'tors
or moving the initialization to an init() method altogether.
But in an init() method I can't use the initialization
list, as it is only allowed in a constructor.
It somehow turns out that all my classes wind up with
at least two constructors and at some point I always have
to resort to an init() method and get rid of the initialization
lists in favor of a screenful of
member1=0;
member2=0;
member3=NULL;
member4=20;
lines.
Am I missing something or do the initialization-lists turn
out to be useless that often?
thanks in advance,
- J.
Suppose I have a class Foo that defines a default c'tor
that initializes some data using an initialization list:
Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
// and so on, quite a few members
{}
and does nothing else.
Then it turns out that I need two more c'tors that would
do some extra initialization apart from the one performed
by the default c'tor, like
Foo::Foo(string filename) {
// do something with filename
// * now wants to initialize the members in the same
// way Foo::Foo() does
}
Foo::Foo(int n) {
// do something with n
// * now wants to initialize the members in the same
// way Foo::Foo() does
}
As I can't call the default c'tor from the other c'tors,
except for a base-class c'tor, I am faced with a choice
of either repeating the initialization lists in all c'tors
or moving the initialization to an init() method altogether.
But in an init() method I can't use the initialization
list, as it is only allowed in a constructor.
It somehow turns out that all my classes wind up with
at least two constructors and at some point I always have
to resort to an init() method and get rid of the initialization
lists in favor of a screenful of
member1=0;
member2=0;
member3=NULL;
member4=20;
lines.
Am I missing something or do the initialization-lists turn
out to be useless that often?
thanks in advance,
- J.