Topic: Constructor with no argument???
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/04/23 Raw View
jrrphd@ix.netcom.com (Julie Ranier) wrote:
> I'm programming with Borland ObjectWindows 5.0. I've seen in example
> code statements like the following:
> new TMyWindow;
> where TMyWindow is a derivative of TWindow. I don't know how this
> works, it has no argument! Shouldn't it have to be like
> new TMyWindow(arg1, arg2, ...);
> or at least
> new TMyWindow();
Nope, constructors with no arguments don't require an empty pair of
parentheses. In other words, the empty parentheses are optional.
Consider what it takes to create objects of non-class types:
int * ip;
char * str;
ip = new int; // Creates an int
str = new char[20]; // Creates a char array
Now consider what supplying an initial value, i.e., invoking the default
constructor, for these two built-in types looks like:
ip = new int(i); // Creates an int initialized to i
str = new char[20]("I'm just a gigolo");
// Creates an initialized char array
(I'm not completely sure of the second line, but it seems to follow
the rules.)
But we run into problems when we try to do the same thing (supplying a
constructor with no args) when defining auto (local) objects rather than
using 'new' to create the objects:
TMyWindow t; // Defines an object
TMyWindow v(); // Not the same thing (constructor with no args)
// but declares a function
int a; // Defines an int, no initial value
int b(); // Declares a func returning int
char c[20]; // Defines a char array, no initial value
char c[20](); // Declares an array of char funcs
So in my opinion, it's safer (less ambiguous) to remove empty parentheses
on constructors and 'new' type specifiers. Use parenthesized initializers
only if you've got one or more constructor args. Or even use the '='
initializer syntax to remove all doubt of your intentions (and let the
compiler worry about optimizing the copy operation):
int a = i; // Defines an int with an initial value
-- David R. Tribble, david.tribble@central.beasys.com --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/23 Raw View
David R Tribble writes:
> jrrphd@ix.netcom.com (Julie Ranier) wrote:
> Now consider what supplying an initial value, i.e., invoking the default
> constructor, for these two built-in types looks like:
> ip = new int(i); // Creates an int initialized to i
> str = new char[20]("I'm just a gigolo");
> // Creates an initialized char array
The default constructor is the one that is invoked without
parentheses, or with (). If you provide an initializer, a
copy-constructor will be used. However, since there are no
copy-constructors for arrays, you cannot initialize an array allocated
with a new expression, so the second line is actually invalid.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jimX.hyslopX@leitchX.com (Jim Hyslop)
Date: 1997/04/23 Raw View
In article <2.2.32.19970422174844.002fad28@central.beasys.com>,
david.tribble@central.beasys.com says...
> str = new char[20]("I'm just a gigolo");
> // Creates an initialized char array
>
> (I'm not completely sure of the second line, but it seems to follow
> the rules.)
Actually, it doesn't - arrays can only use default constructors. You'd
have to write:
str = new char[20];
strcpy(str,"I'm just a gigolo");
--
Jim Hyslop
Xjim.Xhyslop@leitchX.com
remove all X to email me
Welcome to the world of Windows 95, where you click on "Start" to shut
down.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/04/25 Raw View
David R Tribble <david.tribble@central.beasys.com> writes:
|> Now consider what supplying an initial value, i.e., invoking the default
|> constructor, for these two built-in types looks like:
|>
|> ip = new int(i); // Creates an int initialized to i
|> str = new char[20]("I'm just a gigolo");
|> // Creates an initialized char array
|>
|> (I'm not completely sure of the second line, but it seems to follow
|> the rules.)
You're right not to be sure. You cannot provide an initializer for
array types. According to 5.3.4/14, the only types which can have
initializers are class, arithmetic, enumeration, pointer or pointer to
member types. The special rules pertaining to using a string literal to
initialize an array don't apply here, either.
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Paul D. DeRocco" <strip_these_words_pderocco@ix.netcom.com>
Date: 1997/04/18 Raw View
Julie Ranier wrote:
>
> I'm programming with Borland ObjectWindows 5.0. I've seen in example
> code statements like the following:
> new TMyWindow;
> where TMyWindow is a derivative of TWindow. I don't know how this
> works, it has no argument! Shouldn't it have to be like
> new TMyWindow(arg1, arg2, ...);
> or at least
> new TMyWindow();
To be consistent, it "should" be, but it's legal the way it is.
--
Ciao,
Paul D. DeRocco
(Please send e-mail to mail:pderocco@ix.netcom.com instead of the
return address, which has been altered to foil junk mail senders.)
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: dacut@henry.ece.cmu.edu (David A. Cuthbert)
Date: 1997/04/18 Raw View
Julie Ranier <JRRPHD@ix.netcom.com> wrote:
>I'm programming with Borland ObjectWindows 5.0. I've seen in example
>code statements like the following:
> new TMyWindow;
>where TMyWindow is a derivative of TWindow. I don't know how this
>works, it has no argument! Shouldn't it have to be like
> new TMyWindow(arg1, arg2, ...);
>or at least
> new TMyWindow();
>??? Thanks for your help.
Nope. It's another allowable form of initialization. I actually find
it preferable. Consider:
MyType j; // Uninitialized MyType, comparable to (but not the same as)
// MyType* j = new MyType;
MyType k = 2; // Initialized MyType, comparable to MyType* k = new MyType(2)
// (allowable only if the constructor isn't explicit)
MyType l(3); // Same as previous, but allowed even if the constructor
// is explicit
MyType m(); // NOT comparable to MyType* m = new MyType();
The last definition is actually a declaration: m is a function taking
no arguments and returning a MyType. The lack of a corresponding
syntax for "new MyType" for auto objects is what keeps me from using
the (). Conversely, the lack of an exactly corresponding syntax for
"MyType k = 2" for dynamic objects makes me think twice (but I still
use it from time to time).
--
David A. Cuthbert (henry.ece.cmu.edu!dacut)
Graduate Student, Electrical and Computer Engineering
Data Storage Systems Center, Carnegie Mellon University
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: JRRPHD@ix.netcom.com (Julie Ranier)
Date: 1997/04/16 Raw View
I'm programming with Borland ObjectWindows 5.0. I've seen in example
code statements like the following:
new TMyWindow;
where TMyWindow is a derivative of TWindow. I don't know how this
works, it has no argument! Shouldn't it have to be like
new TMyWindow(arg1, arg2, ...);
or at least
new TMyWindow();
??? Thanks for your help.
jrrphd@ix.netcom.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]