Thread View: comp.std.c++
4 messages
4 total messages
Started by Roman Lechtchinsky
Wed, 27 Mar 1996 03:07
"explicit" default constructor?
Author: Roman Lechtchinsky
Date: Wed, 27 Mar 1996 03:07
Date: Wed, 27 Mar 1996 03:07
21 lines
1667 bytes
1667 bytes
Hi, as I read the section [class.conv.ctor] of the DWP, I get the impression that the description of converting and non-converting constructors applies to all constructors, i.e. a constructor is either converting or non-converting. A non-converting constructor creates an object "only where a constructor call is explicitly indicated by the syntax". This has no implications on constructors with more than one parameter, but what about the default constructor? Should a default constructor call be really indicated by the syntax and if so, how is it to be done? What if the default constructor is declared "explicit"? Bye Roman --- [ 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 ]
Re: "explicit" default constructor?
Author: Steve Clamage
Date: Wed, 27 Mar 1996 17:13
Date: Wed, 27 Mar 1996 17:13
31 lines
2248 bytes
2248 bytes
In article F1@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes: >as I read the section [class.conv.ctor] of the DWP, I get the impression that >the description of converting and non-converting constructors applies to all >constructors, i.e. a constructor is either converting or non-converting. A >non-converting constructor creates an object "only where a constructor call >is explicitly indicated by the syntax". This has no implications on >constructors with more than one parameter, but what about the default >constructor? Should a default constructor call be really indicated by the >syntax and if so, how is it to be done? What if the default constructor is >declared "explicit"? A converting constructor is one which can be called with a single parameter. The default constructor isn't a converting constructor except in the case of a constructor with all parameters having default values. If a constructor has no parameters, declaring it "explicit" has no effect, since it can never be invoked for a conversion. If a constructor's parameters all have default values, it can be a converting constructor, and declaring it "explicit" prevents it from being invoked implicitly. --- Steve Clamage, stephen.clamage@eng.sun.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 ]
Re: "explicit" default constructor?
Author: Roman Lechtchinsky
Date: Thu, 28 Mar 1996 09:38
Date: Thu, 28 Mar 1996 09:38
63 lines
3280 bytes
3280 bytes
Steve Clamage wrote:
> In article F1@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
> writes:
>
> > as I read the section [class.conv.ctor] of the DWP, I get the
> > impression that the description of converting and non-converting
> > constructors applies to all constructors, i.e. a constructor is either
> > converting or non-converting. A non-converting constructor creates an
> > object "only where a constructor call is explicitly indicated by the
> > syntax". This has no implications on constructors with more than one
> > parameter, but what about the default constructor? Should a default
> > constructor call be really indicated by the syntax and if so, how is it
> > to be done? What if the default constructor is declared "explicit"?
>
> A converting constructor is one which can be called with a single parameter.
> The default constructor isn't a converting constructor except in the
> case of a constructor with all parameters having default values.
>
> If a constructor has no parameters, declaring it "explicit" has no effect,
> since it can never be invoked for a conversion.
>
Exactly this is my question. Does this section deal only with constructors
with one parameter ( i.e. those that could be converting )? I read it this
way: if a constructor is not a converting constructor, i.e. it either doesn't
have exactly one parameter or is declared explicit, it is a non-converting
constructor and a call should be explicitly indicated by the syntax. In this
case the default constructor is non-converting.
> If a constructor's parameters all have default values, it can be a
> converting constructor, and declaring it "explicit" prevents it from
> being invoked implicitly.
Yes, I've had this in mind, too. In the following case:
class A
{
explicit A( int x=0 );
};
how do you call the default constructor? Can it still be invoked implicitly
and what is the "explicit" syntax if not? IMO, something like
A a;
is not explicit ( maybe I'm wrong here and this does explicitly call the
default constructor explicitly - but where does the DWP define it? ). An
explicit call would rather be
A a();
but what is the point in using this syntax?
Bye
Roman
---
[ 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 ]
Re: "explicit" default constructor?
Author: David Vandevoorde
Date: Thu, 28 Mar 1996 16:28
Date: Thu, 28 Mar 1996 16:28
41 lines
2181 bytes
2181 bytes
>>>>> "RL" == Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
[...]
>> If a constructor's parameters all have default values, it can be a
>> converting constructor, and declaring it "explicit" prevents it from
>> being invoked implicitly.
RL> Yes, I've had this in mind, too. In the following case:
RL> class A
RL> {
RL> explicit A( int x=0 );
RL> };
RL> how do you call the default constructor? Can it still be invoked
RL> implicitly and what is the "explicit" syntax if not? IMO, something like
RL> A a;
RL> is not explicit ( maybe I'm wrong here and this does explicitly call the
RL> default constructor explicitly - but where does the DWP define it? ). An
RL> explicit call would rather be
Yes: the above is quite ``explicit''. Implicit would be:
a = 23; // Implicitly equivalent to `a = A(23)' if no `explicit'
// specification (and no preferred int-assignment
// alternative).
RL> A a();
This is not a constructor invacation anyway (it's the declaration of
a function `a' returning an object of type `A').
Daveed
[ 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 ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads