In the Open Babel library, there are defined many iterator objects for OBMol class, like OBMolAtomiter. On the linked page, there is following code sample illustrating the usage.
#include <openbabel/obiter.h>
#include <openbabel/mol.h>
OpenBabel::OBMol mol;
double exactMass = 0.0;
FOR_ATOMS_OF_MOL(a, mol)
{
// The variable a behaves like OBAtom* when used with -> and * but
// but needs to be explicitly converted when appearing as a parameter
// in a function call - use &*a
exactMass += a->GetExactMass();
}
(FOR_ATOMS_OF_MOL(a, mol) expands into a for cycle, a is declared to be of the iterator type. mol is an existing molecule to iterate over)
I want to ask, why the &*p thing described in the comment is necessary. The behavior when I pass the iterator to a function that expects pointer is that the the code compiles, but the program behaves strangely.
I tried to Google for it, I found the page about iterator_traits, is it anyhow related?
The
FOR_ATOMS_OF_MOL(a, mol)macro constructsaof typeOBMolAtomIter. To returnOBAtoms, the*and->operators have been overloaded. That is whyacannot be transmitted directly to functions, but*aanda->behave as ifawas anOBAtom *.http://openbabel.org/api/2.2.0/classOpenBabel_1_1OBMolAtomIter.shtml