C++:Can one constructor call another from the same object?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 In Java it's quite common to call one constructor from another one in the same objects. Can that be done in C++? Is the same kind of situation handled in C++ with default values for arguments to the constructor rather than having two different constructors? STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFAPzA5wX61+IL0QsMRAjcEAKDqp/Hqt9NjnML+FNhTQ7q+Ps2iGwCfTLgV lChiEZouVk+/GxAcOR6kI9M= =ubT3 -----END PGP SIGNATURE-----
In Java it's quite common to call one constructor from another one in the same objects. Can that be done in C++? Is the same kind of situation handled in C++ with default values for arguments to the constructor rather than having two different constructors? Yes. In general the base class constructor is called implicitly, but you can also call it explicitly. class foo {
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Fri, 27 Feb 2004 06:55:29 -0500 "Steven T. Hatton" <hattons@globalsymmetry.com> wrote: public: foo() {} foo(const string &s): _name(s) {} private: string _name; }; class foobar : public foo { public: foobar() {} // foo() invoked implicitly. foobar(const string &s): foo(s) {} // foo is invoked explicitly } - -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFAP3Di+wA+1cUGHqkRAvy2AJ4uHDB2GmGJzI72vf9fApMkvpmoJACghJK/ AgTQaNRo5fDDBlueoYQ0DaI= =BP8f -----END PGP SIGNATURE-----
Jerry Feldman wrote:
On Fri, 27 Feb 2004 06:55:29 -0500 "Steven T. Hatton" <hattons@globalsymmetry.com> wrote:
In Java it's quite common to call one constructor from another one in the same objects. Can that be done in C++? Is the same kind of situation handled in C++ with default values for arguments to the constructor rather than having two different constructors?
Yes. In general the base class constructor is called implicitly, but you
I think Steven asked if the constructor of the _own_ object can be called inside another constructor. I don't think it is possible. You usually mimic it with defaults values, but I really prefer the Java approach (actually, it is probably the only thing I like in Java). []s Davi
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Monday 01 March 2004 03:46 am, Davi de Castro Reis wrote:
Jerry Feldman wrote:
Yes. In general the base class constructor is called implicitly, but you
I think Steven asked if the constructor of the _own_ object can be called inside another constructor.
Yes, that was my intention. The attempt I made to do so resulted in an error saying the constructor of the class called by the outer constructor was not an immediate base class of the calling class.
I don't think it is possible. You usually mimic it with defaults values, but I really prefer the Java approach (actually, it is probably the only thing I like in Java).
There are several things I like about Java over C++. Since I have far less C++ experience, I can't name as many good things I like about it over Java. I'm still unsure if operator overloading is a good or a bad thing. I would like to see a language called C3 (or something like that). That is C2++. It would be C++ unencumbered by backward compatibility requirements, and would have either a restricted mode, or simply provide a more orthogonal subset of C++. I really believe one of the strengths of Java is its limitation on the number of ways you can accomplish the same thing. Another thing C++ suffers from is the header file. A few days back, I came across a site providing what is ostensibly an automatic header file generator. The programmer simply writes the source code in the source file, and the header file is generated by running the source through the generator. If this is possible for all cases of C++ source files, it suggests to me the header file is indeed a superfluous burdon on the programmer. I think the intention was probably good when the header file was introduced. In the early C days it may have been far more useful than it is today. The idea of laying out your class in a declaration before defining it has advantages, conceptually. OTOH, I can name some negatives resulting from the use of header files: 1) the requirement that the header file and source code be kept in sync adds to the coder's work load, as well as to the opportunity for error. 2) the need to refer back to the header file while composing the source file is distracting, and breaks the coder's train of thought, especially for a person who has not yet internalized that behavior. 3) There are different places such things as default values for arguments can be set. Without standard styles being adapted, we have to look in at least two places to see what's really going on. If the human mind is capable of handling a finite amount of complexity. The more complexity the programming language imposes on our work, that much less capacity is left over for the implementation. To some extent, I believe one of the biggest problems with C++ is that it is typically presented as a variant of C with a complete C-style introduction to the language preceding the introduction of the OO aspects. I present Stroustrup's own selection of examples from TC++PL to support what I've said: http://www.research.att.com/~bs/3rd_code.html Note that his Hello World is not OO. Also note that he does not have a single header file in the entire list of examples.
Davi
STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFAQVv7wX61+IL0QsMRAsZCAKCj0smBXh86MtckhzY+0/vQrZ1IEQCgoPH1 EsW4iRYtkIFsCVjMd9fy7II= =zkFi -----END PGP SIGNATURE-----
On 1 Mar 2004 at 5:46, Davi de Castro Reis wrote: Date sent: Mon, 01 Mar 2004 05:46:49 -0300 From: Davi de Castro Reis <davicastro@terra.com.br> To: Jerry Feldman <gaf@blu.org> Copies to: suse-programming-e@suse.com Subject: Re: [suse-programming-e] C++:Can one constructor call another from the same object?
Jerry Feldman wrote:
On Fri, 27 Feb 2004 06:55:29 -0500 "Steven T. Hatton" <hattons@globalsymmetry.com> wrote:
In Java it's quite common to call one constructor from another one in the same objects. Can that be done in C++? Is the same kind of situation handled in C++ with default values for arguments to the constructor rather than having two different constructors?
Yes. In general the base class constructor is called implicitly, but you
I think Steven asked if the constructor of the _own_ object can be called inside another constructor. I don't think it is possible. You usually mimic it with defaults values, but I really prefer the Java approach (actually, it is probably the only thing I like in Java).
[]s Davi
This is something that's under serious consideration for adding to the next version of C++ :) ISTR it's called forward constructors. alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
participants (4)
-
alan@ibgames.com
-
Davi de Castro Reis
-
Jerry Feldman
-
Steven T. Hatton