Can't incremen int inside funciton call -- bug?

Aldarris aldarri_s at yahoo.com
Tue May 29 04:37:32 PDT 2007


Ary Manzana Wrote:

> Hi.
> 
> Shouldn't that be ++treeLevel?
> 
> In general var++ is an expression that returns var and leaves var with 
> the value var + 1. ++var is an expression that returns var + 1 and 
> leaves var with var + 1.

Thanks.  Should have studied theory better.

> 
> int x = 1;
> int y = x++; // leaves y = 1, x = 2
> 
> int x = 1;
> int y = ++x; // leaves y = 2, x = 2
> 
> Further, your two codes are differentes. In the first one you increment 
> treeLevel in each foreach iteration, while on the second you increment 
> it once before the foreach loop.

Yes, that was the idea -- to send to the function not treeLevel, but a number higher than treeLevel by one and thus leaving treeLevel unchanged to call the function again from the same node.
Incrementing treeLevel elsewhere gives the desired result.
I have just found that the following:
{recursiveXMLTreeIteration(childNode, (treeLevel + 1));}
gives the desired result.

> Best regards,
> Ary
> 
> Aldarris escribió:
> > Hello.
> > 
> > I have a nested function that iterates through a tree recursively.
> > here is it code:
> > 
> > void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
> > {
> > 	writefln(treeLevel);
> > 	tabOffset.length = 0;
> > 	for(int i = 0; i < treeLevel; i++)
> > 		{tabOffset ~= "\t";}
> > .......			
> > 	foreach(XMLNode childNode; currentNode.getChildrenList())
> > 		{recursiveXMLTreeIteration(childNode, treeLevel++);}
> > }
> > 
> > recursiveXMLTreeIteration(this, 0);
> > 
> > Does not work as expected, in a tree of a root node plus two child nodes it prints 0,0,1.
> > 
> > It treeLevel++; is moved outside, works as expected:
> > .......			
> >         treeLevel++;
> > 	foreach(XMLNode childNode; currentNode.getChildrenList())
> > 		{recursiveXMLTreeIteration(childNode, treeLevel)
> > }
> > 
> > 
> > Prints 0,1,1,




More information about the Digitalmars-d mailing list