Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:27:12+00:00 2026-06-07T21:27:12+00:00

I’m working with a particle simulation library. The way interactions are added to particles

  • 0

I’m working with a particle simulation library. The way interactions are added to particles it through the following library function:

AddInteraction(ParticleSet particleSet, void(*interaction)(xyz* p, xyz* v))

I now want to pass a member function to AddInteraction. I’ve understood that this is impossible to do without changing the library function. Changing
the library is something I’d like to avoid, but if the change is small I can mail the author of the library and ask for it to be implemented.

I want to write a simple example of how it can be done. Different solutions are possible:

  • Lambda expressions. These would be perfect, however the library uses CUDA and NVCC, which don’t support Lamda expressions yet.

  • Functors. I thought functors could do the job. However, I haven’t managed to get them working in the way I want. For example,
    I can’t get a list of functors to work. This problem is described in http://www.tutok.sk/fastgl/callback.html under Parameterize the Caller:

‘ If a component has many callback relationships it quickly becomes unworkable to parameterize them all. Consider a Button that wants to maintain a
dynamic list of callees to be notified upon a click event. Since the callee type is built into the Button class type, this list must be either homogeneous or typeless.’

I have to admit that I don’t understand everything that is written on that site, so there might be an answer there.

  • Using a 3d party library. For example the ‘Callbacks Using Template Functors Library’ as described on http://www.tutok.sk/fastgl/callback.html, or using Boost::Function. However, these will probably require major changes to the particle library I’m working with, right?

Are there any alternatives? Or is there a way to get one of the above solutions to work?

Thanks a lot!

Edit:

Thanks for your suggestions. They help, but I fail to see how they give a complete solution to my problem. Specifically, I’m trying to get this to work:

std::vector<void(*)(xyz*,xyz*)> interactionList;

void AddInteraction(void(*func)(xyz*,xyz*))
{
    interactionList.push_back(func);
}

void Simulate()
{
    for(size_t i = 0; i < interactionList.size(); i++)
    {
        interactionList[i](0,0); //Would normally be called for every particle
    }
}

class Cell {
public:
    void UpdateParticle(xyz* p, xyz* v);
    Cell()
    {
        AddInteraction(this->UpdateParticle); //Does not work
    }
};

int main()
{
    Cell cell1;
    Cell cell2;

    for(int i = 0; i < 100; i++)
    {
        Simulate();
    }

    return 1;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:27:15+00:00Added an answer on June 7, 2026 at 9:27 pm

    Typically, callback function have a void * argument that allows client code a placeholder for any other information it may require.

    This way, the client code can pass in anything they want and recast it back to the original type when the callback is invoked. Note, the calling code knows the original type.

    This type of interface allows C only code to work but it’s pretty easy to wrap this in a C++ object if needed. At the very least, the library author should provide this.

    EDIT TO ANSWER OPs COMMENT
    Below I’ve modified the Cell class appropriately.

    class Cell  
    { 
    public:
      static void UpdateParticle(void *stuff, xyz* p, xyz* v)  // this has to be static as others have mentioned, note the additional void * argument
      {
          Cell * c = (Cell *) stuff;
          // do work
      }  
    
      Cell()
      {
          AddInteraction(this, UpdateParticle); // note this takes a two items, both of which have to be saved for future use by AddInteraction and utilized by Simulate
      }
    };  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.