 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Potter Guest
|
Posted: Fri Jun 27, 2003 4:48 pm Post subject: Re: initializer form |
|
|
On 26 Jun 2003 10:15:52 -0400, "Andrei Alexandrescu"
<SeeWebsiteForEmail (AT) moderncppdesign (DOT) com> wrote:
| Quote: | "John Potter" <jpotter (AT) falcon (DOT) lhup.edu> wrote in message
news:saljfvci4u0qura56gr7kbm69ldo4ernn7 (AT) 4ax (DOT) com...
Never use the assignment symbol for initialization, it confuses
too many beginners.
Sure I must've misunderstood something. I see nothing confusing about:
int i = 5;
float f = 6;
string s = "Wyda!";
|
FAQ: Why aren't the default ctor and assignment operator called?
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Sat Jun 28, 2003 11:26 pm Post subject: Re: initializer form |
|
|
John Potter <jpotter (AT) falcon (DOT) lhup.edu> writes:
| Quote: | On 26 Jun 2003 10:15:52 -0400, "Andrei Alexandrescu"
[email]SeeWebsiteForEmail (AT) moderncppdesign (DOT) com[/email]> wrote:
"John Potter" <jpotter (AT) falcon (DOT) lhup.edu> wrote in message
news:saljfvci4u0qura56gr7kbm69ldo4ernn7 (AT) 4ax (DOT) com...
Never use the assignment symbol for initialization, it confuses
too many beginners.
Sure I must've misunderstood something. I see nothing confusing about:
int i = 5;
float f = 6;
string s = "Wyda!";
FAQ: Why aren't the default ctor and assignment operator called?
[snip] |
Unfortunately, for types convertible to bool, there is a place where
constructor style initialization cannot be used:
if(int i(foo)) //syntax error
{
/* ... */
}
if(int i= foo) //ok
{
/* ... */
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ed Avis Guest
|
Posted: Sun Jun 29, 2003 2:03 pm Post subject: Re: initializer form |
|
|
llewelly <llewelly.at (AT) xmission (DOT) dot.com> writes:
| Quote: | Unfortunately, for types convertible to bool, there is a place where
constructor style initialization cannot be used:
if(int i(foo)) //syntax error
{
/* ... */
}
if(int i= foo) //ok
{
/* ... */
}
|
That seems really unfortunate, given the mistake (made occasionally by
even experienced programmers) of writing = for == or vice versa. If
the first form were legal then compilers could warn about the second.
What syntactic problem arises if the constructor style is permitted?
--
Ed Avis <ed (AT) membled (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Mon Jun 30, 2003 4:00 pm Post subject: Re: initializer form |
|
|
Ed Avis <ed (AT) membled (DOT) com> writes:
| Quote: | llewelly <llewelly.at (AT) xmission (DOT) dot.com> writes:
Unfortunately, for types convertible to bool, there is a place where
constructor style initialization cannot be used:
if(int i(foo)) //syntax error
{
/* ... */
}
if(int i= foo) //ok
{
/* ... */
}
That seems really unfortunate, given the mistake (made occasionally by
even experienced programmers) of writing = for == or vice versa. If
the first form were legal then compilers could warn about the second.
What syntactic problem arises if the constructor style is permitted?
|
I do not know of any, except the most vexing parse, which is always an
issue with constructor style, in or out of an if. I think this
case was simply overlooked.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
KIM Seungbeom Guest
|
Posted: Tue Jul 01, 2003 8:19 am Post subject: Re: initializer form |
|
|
Ed Avis <ed (AT) membled (DOT) com> wrote
| Quote: | llewelly <llewelly.at (AT) xmission (DOT) dot.com> writes:
Unfortunately, for types convertible to bool, there is a place where
constructor style initialization cannot be used:
if(int i(foo)) //syntax error
{
/* ... */
}
if(int i= foo) //ok
{
/* ... */
}
That seems really unfortunate, given the mistake (made occasionally by
even experienced programmers) of writing = for == or vice versa. If
the first form were legal then compilers could warn about the second.
|
What is the problem with the second approach?
If you use == instead of = by mistake, it will be a syntax error.
if (int i == foo) // error
{ /* ... */
| Quote: |
What syntactic problem arises if the constructor style is permitted?
|
I don't see any problem.
--
KIM Seungbeom <musiphil (AT) bawi (DOT) org>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|