Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define predicate within class

Tags:

c++

stl

predicate

I have a class with a method which I would also like to be able to use as a predicate.

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); }
  };
};

However, the compiler complains about not being able to use ParticleHasAncestor() without an instance of MyClass. Do I need to use a friend class? Or is there a better solution to this?

I am not using C++11, so cannot use lambda functions.

Update: ParticleHasAncestor() cannot be made static since it uses members of MyClass.

like image 503
David Hall Avatar asked Aug 10 '26 06:08

David Hall


1 Answers

Make this predicate a static method. But it cannot be const then.

like image 167
cpp Avatar answered Aug 12 '26 20:08

cpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!