Topic: Constructor call from a constructor?
Author: "Niklas Pettersson" <npedt97@student.vxu.se (nospam)>
Date: 2000/04/27 Raw View
Hello!
In a class(date) that I'm writing I have 3 costructors. They essentially
performs the same task (but takes the different representations of the
date). So I made a constructor "chief" and let the other constructors call
the chief constructor (after converting to correct representation). My
compiler (GCC) didn't complain but the results from the program was not
funny.. So I made an ordinary function and called that one from all
constructors instead and now it works.. Is this a bug in GCC or is it
illegal to call a constructor from whithin another constructor?
/ Niklas
I found a bug in a program that I'm writing, the cause of the error was that
I called
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/04/27 Raw View
In article <8e4im3$dec$1@news.lth.se>, Niklas Pettersson
<npedt97@student.vxu.se> writes
>In a class(date) that I'm writing I have 3 costructors. They essentially
>performs the same task (but takes the different representations of the
>date). So I made a constructor "chief" and let the other constructors call
>the chief constructor (after converting to correct representation). My
>compiler (GCC) didn't complain but the results from the program was not
>funny.. So I made an ordinary function and called that one from all
>constructors instead and now it works.. Is this a bug in GCC or is it
>illegal to call a constructor from whithin another constructor?
The simple answer is that it is illegal (you cannot do what I believe
Java allows). The fix is what you did (though you should consider
making that initialisation function private or protected).
I suspect that GCC did not complain because your code was syntactically
correct but did not do what you expected. I think you created an
entirely local version of the object in the ctor body that was destroyed
on exit, leaving the real object uninitialised.
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Daniel M. Pfeffer" <pfefferd@nospam.internet-zahav.net>
Date: 2000/04/28 Raw View
I assume that your code looked as follows:
class Foo {
public:
Foo() { Foo(0); }
Foo(int i)
: i_(i) {}
private:
int i_;
}
The call to Foo(int) inside Foo() constructs a new (unnamed) object on the
stack, which is deleted when Foo() returns (possibly beforehand?). It is
totally unrelated to the object being constructed by Foo().
Your solution is correct - provide an initialisation function that does the
work for both costructors:
class Foo {
public:
Foo() { init(0); }
Foo(int i) { init(i); }
private:
void init(int i) { i_ = i; }
int i_;
}
Daniel Pfeffer
"Niklas Pettersson" <npedt97@student.vxu.se (nospam)> wrote in message
news:8e4im3$dec$1@news.lth.se...
> Hello!
>
> In a class(date) that I'm writing I have 3 costructors. They essentially
> performs the same task (but takes the different representations of the
> date). So I made a constructor "chief" and let the other constructors call
> the chief constructor (after converting to correct representation). My
> compiler (GCC) didn't complain but the results from the program was not
> funny.. So I made an ordinary function and called that one from all
> constructors instead and now it works.. Is this a bug in GCC or is it
> illegal to call a constructor from whithin another constructor?
>
> / Niklas
>
>
> I found a bug in a program that I'm writing, the cause of the error was
that
> I called
>
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/28 Raw View
Francis Glassborow wrote:
>
> In article <8e4im3$dec$1@news.lth.se>, Niklas Pettersson
> <npedt97@student.vxu.se> writes
> >In a class(date) that I'm writing I have 3 costructors. They essentially
> >performs the same task (but takes the different representations of the
> >date). So I made a constructor "chief" and let the other constructors call
> >the chief constructor (after converting to correct representation). My
> >compiler (GCC) didn't complain but the results from the program was not
> >funny.. So I made an ordinary function and called that one from all
> >constructors instead and now it works.. Is this a bug in GCC or is it
> >illegal to call a constructor from whithin another constructor?
>
> The simple answer is that it is illegal (you cannot do what I believe
> Java allows). The fix is what you did (though you should consider
> making that initialisation function private or protected).
An alternative approach that is sometimes useful is to have a base class
with the general constructor, and a derived class with the specialized
constructors that call the general one. This is clumsy, however, unless
you have in addition some other reason for splitting the class into a
base part and a derived part.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]