0

I have 2 classes: MeshInput and VoxelGrid. VoxelGrid has the member sdf which is a function pointer. Now I want to assign a non static function of another class to sdf. I can't change the signature of either because sdf also sometimes points to a function outside of MeshInput. How do I do that?

Code below:

class VoxelGrid {
 private:
  double (*sdf)(tg::pos3 const&);
}

class MeshInput {
 public:
  double sdfFromMesh(tg::pos3 const& point);
}

From my understanding the Issue is that when calling the function with the function pointer there is no implicit this* pointer like when calling it from an object (A.sdfFromMesh()). But I don't know how to work around that.

4
  • 1
    You cannot do this. Non static member function doesn't have double (tg::pos3 const&) signature, its first argument is a pointer to the object. Can you change your raw pointer to std::function<double(tg::pos3 const&)> ? Commented Nov 16, 2023 at 9:42
  • theres no way to work around. If you want to call a non static member function you need an object. You could bind the object together with the member function pointer though Commented Nov 16, 2023 at 9:47
  • Why not just make it virtual? Commented Nov 16, 2023 at 9:59
  • How can I pass a member function where a free function is expected? Commented Nov 16, 2023 at 10:47

1 Answer 1

0

You can use std::function cooperate with std::bind.

std::bind can specific target function to be kept and where it belong. For example:

struct Foo
{
    double get(tg::pos3 const&) { return 0; }
};

Then you can bind get to your variable like this:

std::function<double ( tg::pos3 const& )> sdf = std::bind(&Foo::get, &YourFooObj, std::placeholders::_1)

see std::bind and std::function for more detail.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.