On Wed, 25 Jun 2003 11:42:20 +0200, "Giulio" <giulio.gL E V
why definition of two constructors like these is not possible in c++???
The construction model in C++ is quite strict (mostly due to the fact
that C++ has "value-based" UDTs rather than just pointers, as in
Java). Basically, by the time you enter the body of your constructor,
all of your base classes and all of your member variables will already
have been constructed. So there is no way to invoke another
constructor from the body of your constructor, because that would
involve double construction of all your variables.
If you just want to share just the body of the constructor, then
refactor it out into a separate function that both of your
constructors call.
-----------------------
date::date(const int d, const int m, const int y, const int ora, const int
mi, const int se){
day_ = d;
month_ = m;
year_ = y;
secondo_ = se;
minuto_ = mi;
ora_ = ora;
}
Normally that would be done using an initializer list:
date::date(const int d, const int m, const int y, const int ora, const
int mi, const int se)
:day_(d),
month_(m),
year_(y),
secondo_(se),
minuto_(mi),
ora_(ora)
{
}
date::date(){ date(0,0,0,0,0,0); }
date::date()
:day_(0),
month_(0),
year_(0),
secondo_(0),
minuto_(0),
ora_(0)
{
}
As you can see, there is no code in the body at all, so there's no
common code to share anyway.
If you want to do it your way, do it like this:
void date::initialise(const int d, const int m, const int y, const int
ora, const int mi, const int se)
{
day_ = d;
month_ = m;
year_ = y;
secondo_ = se;
minuto_ = mi;
ora_ = ora;
//any checking?
}
date::date()
{
initialise(0,0,0,0,0,0);
}
date::date(const int d, const int m, const int y, const int ora, const
int mi, const int se)
{
initialise(d, m, y, ora, mi, se);
}
but for the level of code sharing involved, it doesn't seem worth it.
Tom