gpt4 book ai didi

c++ - 在类中定义谓词

转载 作者:太空狗 更新时间:2023-10-29 20:42:26 25 4
gpt4 key购买 nike

我有一个带有方法的类,我也希望能够将其用作谓词。

class MyClass {
bool ParticleHasAncestor(const Particle &particle, int id) const;

class AncestorPredicate {
int mId;
public:
AncestorPredicate(int idCode) : mId(idCode) { }
bool operator()(const Particle &particle) const { return ParticleHasAncestor(particle, mId); }
};
};

但是,编译器提示在没有 MyClass 实例的情况下无法使用 ParticleHasAncestor()。我需要使用 friend 类吗?或者对此有更好的解决方案吗?

我没有使用 C++11,所以不能使用 lambda 函数。

更新 ParticleHasAncestor() 不能设为静态,因为它使用了 MyClass 的成员。

最佳答案

使这个谓词成为一个静态方法。但它不能是const

关于c++ - 在类中定义谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18509784/

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