public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* "self" constructor
@ 2000-03-22 17:22 Min Xu
  2000-03-22 17:53 ` Alexandre Oliva
  2000-03-23  0:59 ` Martin v. Loewis
  0 siblings, 2 replies; 7+ messages in thread
From: Min Xu @ 2000-03-22 17:22 UTC (permalink / raw)
  To: gcc

Hi all,
  I used to be a java programmer before I recently graduated into c++. I

have a question about calling a constructor of a class from another
constructor of this class. I already searched the archives of this
mailing
list, however, I can't find answers about it.
  My question in detail is that why following code is rejected by gcc?
(my version is 2.95.2)

// test class for "self" constructor
class test {
private:
    int i;
    int j;
public:
    test(int ii):i(ii) {};
    test(int ii, int jj):test(ii), j(jj) {};
}

  I know this code is too much java-like, but my point is why it is
disallowed?
I guest "super()" isn't in c++ is because c++ has multi-inheritance. But
why not
"this()"?

  Thank you for your attention for reading my question.
Min

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "self" constructor
  2000-03-22 17:22 "self" constructor Min Xu
@ 2000-03-22 17:53 ` Alexandre Oliva
  2000-03-23  0:59 ` Martin v. Loewis
  1 sibling, 0 replies; 7+ messages in thread
From: Alexandre Oliva @ 2000-03-22 17:53 UTC (permalink / raw)
  To: mxu; +Cc: gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 611 bytes --]

On Mar 22, 2000, Min Xu <mxu@cae.wisc.edu> wrote:

> I have a question about calling a constructor of a class from
> another constructor of this class.  My question in detail is that
> why following code is rejected by gcc?

Because, unlike Java, C++ (as of the latest ANSI/ISO Standard) does
not provide this feature.

-- 
Alexandre Oliva    Enjoy Guaraná, see http://www.ic.unicamp.br/~oliva/
Cygnus Solutions, a Red Hat company        aoliva@{redhat, cygnus}.com
Free Software Developer and Evangelist    CS PhD student at IC-Unicamp
oliva@{lsd.ic.unicamp.br, gnu.org}   Write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "self" constructor
  2000-03-22 17:22 "self" constructor Min Xu
  2000-03-22 17:53 ` Alexandre Oliva
@ 2000-03-23  0:59 ` Martin v. Loewis
  2000-03-23  6:35   ` Min Xu
  1 sibling, 1 reply; 7+ messages in thread
From: Martin v. Loewis @ 2000-03-23  0:59 UTC (permalink / raw)
  To: mxu; +Cc: gcc

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "self" constructor
  2000-03-23  0:59 ` Martin v. Loewis
@ 2000-03-23  6:35   ` Min Xu
  2000-03-23  9:21     ` Martin v. Loewis
  0 siblings, 1 reply; 7+ messages in thread
From: Min Xu @ 2000-03-23  6:35 UTC (permalink / raw)
  To: Martin v. Loewis; +Cc: gcc

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "self" constructor
  2000-03-23  6:35   ` Min Xu
@ 2000-03-23  9:21     ` Martin v. Loewis
  0 siblings, 0 replies; 7+ messages in thread
From: Martin v. Loewis @ 2000-03-23  9:21 UTC (permalink / raw)
  To: mxu; +Cc: gcc

> 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?

No particular list. The gcc list is probably best. Please also have a
look

http://gcc.gnu.org/cgi-bin/fom.cgi?file=12

I think it is unlikely that option 2 will bring you anywhere, so
you'll have to consider option 1 and option 3.

I personally think it would be a bad thing to have such an extension,
and will take no further steps in making that happen. In fact, I will
argue against it should a patch be provided.

Regards,
Martin

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "self" constructor
  2000-03-22 19:07 Mike Stump
@ 2000-03-23  6:40 ` Min Xu
  0 siblings, 0 replies; 7+ messages in thread
From: Min Xu @ 2000-03-23  6:40 UTC (permalink / raw)
  To: Mike Stump; +Cc: gcc

Mike Stump wrote:

> > Date: Wed, 22 Mar 2000 19:22:18 -0600
> > From: Min Xu <mxu@cae.wisc.edu>
>
> > I have a question about calling a constructor of a class from
> > another constructor of this class. I already searched the archives
> > of this mailing list, however, I can't find answers about it.
>
> That's because this list isn't for discussing how to program in the
> C++ programming language.  I believe you want the comp.lang.c++ forum.
> Give that a try, I think you'll find an answer over there, thanks.

Oh, I'll give the new group a try. Thank you for your reminder, I hope I
didn't abuse the list.
Sincerely,
Min

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "self" constructor
@ 2000-03-22 19:07 Mike Stump
  2000-03-23  6:40 ` Min Xu
  0 siblings, 1 reply; 7+ messages in thread
From: Mike Stump @ 2000-03-22 19:07 UTC (permalink / raw)
  To: gcc, mxu

> Date: Wed, 22 Mar 2000 19:22:18 -0600
> From: Min Xu <mxu@cae.wisc.edu>

> I have a question about calling a constructor of a class from
> another constructor of this class. I already searched the archives
> of this mailing list, however, I can't find answers about it.

That's because this list isn't for discussing how to program in the
C++ programming language.  I believe you want the comp.lang.c++ forum.
Give that a try, I think you'll find an answer over there, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2000-03-23  9:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-22 17:22 "self" constructor Min Xu
2000-03-22 17:53 ` Alexandre Oliva
2000-03-23  0:59 ` Martin v. Loewis
2000-03-23  6:35   ` Min Xu
2000-03-23  9:21     ` Martin v. Loewis
2000-03-22 19:07 Mike Stump
2000-03-23  6:40 ` Min Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).