[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Need help in understanding for loop in c++

silverburgh.meryl@gmail.com

11/2/2008 3:34:00 AM

Hi,

I need some help in understanding the following for() loop:

for (PRBool isRoot;
mCSSUtils->IsRuleNodeRoot(ruleNode, &isRoot), !isRoot;
mCSSUtils->GetRuleNodeParent(ruleNode, &ruleNode))

what is the middle clause for?
Why there is a ',' in the middle? And what does that mean?


2 Answers

Jerry Coffin

11/2/2008 5:06:00 AM

0

In article <97be6af8-1d3a-4e82-9a4f-751377740b86
@z6g2000pre.googlegroups.com>, silverburgh.meryl@gmail.com says...
> Hi,
>
> I need some help in understanding the following for() loop:
>
> for (PRBool isRoot;
> mCSSUtils->IsRuleNodeRoot(ruleNode, &isRoot), !isRoot;
> mCSSUtils->GetRuleNodeParent(ruleNode, &ruleNode))
>
> what is the middle clause for?
> Why there is a ',' in the middle? And what does that mean?

It looks like this is attempting to traverse from a current node to a
root node in a threaded tree, or something on that general order.

The middle clause is a test, just like usual. The comma operator
evaluates its left operand, discards the result, then evaluates the
right operand. The result of the right operand becomes the result of the
whole expression.

In this case, it's calling "IsRuleNodeRoot", which apparently looks at a
node, figures out whether it's a root node, and sets some sort of semi-
boolean (a pointer to which is passed as a the second parameter) to tell
you that result. In this case, that variable (isRoot) is evaluated as
the right operand of the comma operator, so what it's doing it calling
the function to set the variable, then evaluating the variable itself to
determine whether it's reached the root yet, and continuing the loop
until it reaches the root.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Pete Becker

11/2/2008 10:41:00 AM

0

On 2008-11-02 01:06:17 -0400, Jerry Coffin <jcoffin@taeus.com> said:

>
> In this case, it's calling "IsRuleNodeRoot", which apparently looks at a
> node, figures out whether it's a root node, and sets some sort of semi-
> boolean (a pointer to which is passed as a the second parameter) to tell
> you that result. In this case, that variable (isRoot) is evaluated as
> the right operand of the comma operator, so what it's doing it calling
> the function to set the variable, then evaluating the variable itself to
> determine whether it's reached the root yet, and continuing the loop
> until it reaches the root.

In other words, that comma operator is a workaround for a design error:
there should be a function that returns a boolean that can be used
directly.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)