
February 9th, 2006, 08:55 PM
| | | Places to declare variables
Hi,
I recently noticed that my compiler (VC++7.1) allows me to declare
variables inside the expression for an if-statement, like so:
if (int i = 0)
{
i = 1;
}
Now I was wondering if this is valid code (reference to the
appropriate section/paragraph in the Standard would be nice, as I do not
really know where to look for this kind of thing) and where else I am
able to declare variables (except the obvious "for (int i ...)" case).
For the record, I am asking this out of interest, not because I want
to start declaring my variables in all kinds of funny places :)
Thanks in advance!
--
jb
(reply address in rot13, unscramble first) | 
February 9th, 2006, 09:05 PM
| | | Re: Places to declare variables
Jakob Bieling wrote:[color=blue]
> Hi,
>
> I recently noticed that my compiler (VC++7.1) allows me to declare
> variables inside the expression for an if-statement, like so:
>
> if (int i = 0)
> {
> i = 1;
> }[/color]
if ((int x = calcSomething()) == y)
{
doSomethingWith(x);
}
.... code not accessing x.
You could even do something like this:
int x = 5;
if ((int x = calcSomething()) == y)
{
doSomethingWithCalced(x);
}
assert(x == 5);
.... This code runs...;
Standard defines 'x' to have scope only within the following block of
code and it overrides the upper-scope definitions.
[color=blue]
>
> reference to the
> appropriate section/paragraph in the Standard would be nice, as I do not
> really know where to look for this kind of thing[/color]
3.3.2 paragraph 4 is one such reference but only explains scope. | 
February 9th, 2006, 09:05 PM
| | | Re: Places to declare variables
Jakob Bieling wrote:[color=blue]
> Hi,
>
> I recently noticed that my compiler (VC++7.1) allows me to declare
> variables inside the expression for an if-statement, like so:
>
> if (int i = 0)
> {
> i = 1;
> }
>
> Now I was wondering if this is valid code (reference to the
> appropriate section/paragraph in the Standard would be nice, as I do not
> really know where to look for this kind of thing) and where else I am
> able to declare variables (except the obvious "for (int i ...)" case).
>
> For the record, I am asking this out of interest, not because I want
> to start declaring my variables in all kinds of funny places :)
>
> Thanks in advance!
> --
> jb
>
> (reply address in rot13, unscramble first)[/color]
That is certainly legal. I don't have the standard handy, but see
Stroustrup _TC++PL_ 3rd ed. on dynamic_cast. He does something like
this, which is where the construction is most useful:
if( Derived* pd = dynamic_cast<Derived*>( pBase ) )
{
// pd is valid; do something with it here
}
else
{
// pd is 0 since the dynamic_cast failed;
// do something else here, e.g.
pd = new Derived;
// ...
}
// pd is now out of scope
Cheers! --M | 
February 9th, 2006, 09:15 PM
| | | Re: Places to declare variables
Jakob Bieling wrote:[color=blue]
> Hi,
>
> I recently noticed that my compiler (VC++7.1) allows me to declare
> variables inside the expression for an if-statement, like so:
>
> if (int i = 0)
> {
> i = 1;
> }[/color]
Keep in mind though that the above code does nothing worthwhile as the
value of "int i = 0" is the assigned value; in this case 0, which is
"false". So, i is created, initialized to 0, compared to 0, and then
goes away. The compiler might even toss all that code out. | 
February 9th, 2006, 09:25 PM
| | | Re: Places to declare variables
Jakob Bieling wrote:[color=blue]
> Hi,
>
> I recently noticed that my compiler (VC++7.1) allows me to declare
> variables inside the expression for an if-statement, like so:
>
> if (int i = 0)
> {
> i = 1;
> }
>
> Now I was wondering if this is valid code (reference to the
> appropriate section/paragraph in the Standard would be nice, as I do not
> really know where to look for this kind of thing) and where else I am
> able to declare variables (except the obvious "for (int i ...)" case).[/color]
See 6.4/3. | 
February 9th, 2006, 11:45 PM
| | | Re: Places to declare variables
"Jakob Bieling" <argfhesNGtzkQBGarg@rot13.com> schrieb im Newsbeitrag
news:dsg986$i06$01$1@news.t-online.com...[color=blue]
> Now I was wondering if this is valid code (reference to the appropriate
> section/paragraph in the Standard would be nice, as I do not really know
> where to look for this kind of thing) and where else I am able to declare
> variables (except the obvious "for (int i ...)" case).
>
> For the record, I am asking this out of interest, not because I want to
> start declaring my variables in all kinds of funny places :)[/color]
There is only one funny place to declare/define a variable -- at the top of
a function body. Variables should be defined as late as possible, and that
is the time of its first use. And of cause a variable should always be
initialized immediately.
That's one of the reason why in C++ you can define a variable almost
everywhere, where you can write a statement, and even at some other places
for statements. The only thing you must not do, you must never jump over a
definition. So the following code is not valid:
switch (something)
{
case 1:
int i = 1;
...
break;
case 2:
...
}
You cannot define a variable inside a switch, but if you write clean code
and such a variable is only used in one case, you can make it valid by using
a compound statement, which restricts the scope of such a variable:
switch (something)
{
case 1:
{
int i = 1;
...
}
break;
case 2:
...
}
Regards
Heinz | 
February 10th, 2006, 12:55 AM
| | | Re: Places to declare variables
Heinz Ozwirk wrote:[color=blue]
> That's one of the reason why in C++ you can define a variable almost
> everywhere, where you can write a statement, and even at some other places
> for statements. The only thing you must not do, you must never jump over a
> definition. So the following code is not valid:
>
> switch (something)
> {
> case 1:
> int i = 1;
> ...
> break;
> case 2:
> ...
> }[/color]
That's interesting.
[color=blue]
> You cannot define a variable inside a switch, but if you write clean code
> and such a variable is only used in one case, you can make it valid by using
> a compound statement, which restricts the scope of such a variable:
>
> switch (something)
> {
> case 1:
> {
> int i = 1;
> ...
> }
> break;
> case 2:
> ...
> }[/color]
I have a habit of creating scope here anyway, in much the same way that
do it for the if construct.
Ben Pope
--
I'm not just a number. To many, I'm known as a string... |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|