I'm not really versed in C++, but I came to the following code:
BaseManager* allManagers[] = { mColorManager, mToolManager, mLayerManager, mPlaybackManager, mViewManager }; for ( BaseManager* pManager : allManagers ) { pManager->setEditor( this ); pManager->init(); } I use an older g++, so I cannot use -std=c++11, and I have to use -std=c++0x. Looking at the "old-school" equivalent in error: expected initializer before ‘:’ token, I would have hoped the following would work:
for ( auto it = allManagers.begin(); it != allManagers.end(); ++it ) { BaseManager* pManager = *it; pManager->setEditor( this ); pManager->init(); } ... but it fails with:
error: request for member ‘begin’ in ‘allManagers’, which is of non-class type ‘BaseManager* [5]’ error: unable to deduce ‘auto’ from ‘<expression error>’ So I gathered, since this allManagers is apparently just a C array, it is not an object with methods (like .begin) - so in the end, I finally got that piece to compile with:
for ( int i=0 ; i<5 ; i++ ) { BaseManager* pManager = allManagers[i]; pManager->setEditor( this ); pManager->init(); } ... however, that requires me to write in the array length manually, which I don't like.
So, my question is: what would be the proper way to iterate through such an array of pointers without using a range-based for loop - but also, without having to explicitly enter the hardcoded array length?
for ( int i = 0; i < (sizeof(allManagers) / sizeof(allManagers[0])); ++i )is a C-ish way to do it.