gpt4 book ai didi

c++ - Open Babel C++ API 为什么他们的自定义迭代器不能直接用作函数的参数?

转载 作者:行者123 更新时间:2023-11-28 08:05:50 25 4
gpt4 key购买 nike

在 Open Babel 库中,为 OBMol 类定义了许多迭代器对象,如 OBMolAtomiter .在链接页面上,有以下代码示例说明了用法。

  #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)展开成for循环,a声明为迭代器类型。mol是一个要迭代的现有分子)

我想问一下,为什么评论中描述的 &*p 东西是必要的。当我将迭代器传递给需要指针的函数时的行为是代码编译,但程序行为异常。

我试着用谷歌搜索它,我找到了关于 iterator_traits 的页面, 它有什么关系吗?

最佳答案

FOR_ATOMS_OF_MOL(a, mol) 宏构造 a OBMolAtomIter 类型。为了返回 OBAtom*-> 运算符已被重载。这就是为什么 a 不能直接传递给函数,但是 *aa-> 的行为就好像 a是一个 OBAtom *

http://openbabel.org/api/2.2.0/classOpenBabel_1_1OBMolAtomIter.shtml

关于c++ - Open Babel C++ API 为什么他们的自定义迭代器不能直接用作函数的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10320188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com