This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: "self" constructor
- To: mxu at cae dot wisc dot edu
- Subject: Re: "self" constructor
- From: "Martin v. Loewis" <martin at loewis dot home dot cs dot tu-berlin dot de>
- Date: Thu, 23 Mar 2000 09:25:09 +0100
- CC: gcc at gcc dot gnu dot org
- References: <38D971CA.BED14EFD@cae.wisc.edu>
> I guest "super()" isn't in c++ is because c++ has
> multi-inheritance. But why not "this()"?
Instead of super, you'll have to use the name of the base class, in
the colon member initializer list. However, delegation of one
constructor to another is not supported in C++. Consider
class test:Base {
private:
int i;
int j;
public:
test(int ii):Base("Hello"), i(ii) {};
test(int ii, int jj):Base(), test(ii), j(jj) {};
}
Base classes are initialized before derived classes, and the
constructor seems to say to use the parameter-less constructor for
initializing base. Then you jump to the other ctor, and see a
different base initialization???
Anyway, it is not supported - the typical solution is to make a
private method to hold the shared code, and to invoke that from each
ctor.
You may find that you can remove a number of ctors by providing
default arguments:
class test {
private:
int i;
int j;
public:
test(int ii, int jj=0):i(ii), j(jj) {};
}
Also note that member variables are *not* normally zero-initialized in
C++.
Regards,
Martin