Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a for loop containing multiple type declarations?

Tags:

c++

For example:

Is there anything I can do, that might allow me to do this:

for(TiXmlElement * pChild = elem->First(), int i=0; // note multiple type declarations
    pChild; 
    pChild=pChild->NextSiblingElement(), i++) // note multiple types
{
    //do stuff
}

Perhaps there is a boost header?

like image 705
BeeBand Avatar asked Dec 06 '22 23:12

BeeBand


2 Answers

Nope.

If you want to limit the scope of variables to the loop just add another scope:

{
    TiXmlElement * pChild = elem->First();
    int i = 0;
    for(; pChild; pChild=pChild->NextSiblingElement(), i++)
    {
        //do stuff
    }
}
like image 78
Maxim Egorushkin Avatar answered Mar 30 '23 00:03

Maxim Egorushkin


Blocks do not have to be attached to functions or conditionals. You can surround any piece of code with a block to limit the scope of temporary variables to that block.

{
    TiXmlElement * pChild;
    int i;
    for ( pChild = elem->First(), i = 0;
          pChild;
          pChild = pChild->NextSiblingElement(), ++i )
    {
        // do stuff
    }
}
like image 43
DevSolar Avatar answered Mar 29 '23 22:03

DevSolar