"self" constructor
Min Xu
mxu@cae.wisc.edu
Thu Mar 23 06:35:00 GMT 2000
Thank you for your very detailed and clear explanations. Now I
know it is not supported in ANSI c++.
In addition, as a user of gcc, I wish this feature can be put into
the compile as a "extension". That's why I send this question to
this email list. Is there any wishes list for gcc?
thanks again and regards,
Min
"Martin v. Loewis" wrote:
> > 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
More information about the Gcc
mailing list