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 8289449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:30:38+00:00 2026-06-08T12:30:38+00:00

Basically, I have a templated class that runs some algorithm, and that algorithm needs

  • 0

Basically, I have a templated class that runs some algorithm, and that algorithm needs a similarity(T t1, T t2) function that returns a double defining how similar two objects of type T are. This similarity function varies greatly depending on what T is defined as, so the caller of this class needs to define the similarity function. I assume a function pointer is in order, but how do I save a function from a function pointer as a member function for use in the algorithm?

That is, I want to pass in similarity(T t1, T t2) in the class’ constructor and be able to call that as a member function throughout the class. How do I do that? Thanks

  • 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-08T12:30:39+00:00Added an answer on June 8, 2026 at 12:30 pm

    You can’t add member functions to a class at runtime.

    You could write a member function that calls the supplied function via the function pointer:

    class MyClass {
        typedef double (*similarity_fn_type)(T t1, T t2);
        similarity_fn_type similarity_fn;
      public:
        MyClass(similarity_fn_type ptr) : similarity_fn(ptr) {}
        double similarity(T t1, T t2) {
            return similarity_fn(t1, t2);
        }
    };
    

    Alternatively, you could make the similarity_fn member public. The syntax for calling it would make it look like a member function, but you might not consider it to be great encapsulation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a class that outputs some html: class Foo include ActionView::Helpers def
That may sound a little confusing. Basically, I have a function CCard newCard() {
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
I have a template function in C++ that basically writes values to an XML
I wondering if something similar to this is possible. Basically, I have a templated
I have a base class that basically wraps up attaching a class to a
In a project I'm working on, I have a fairly large templated class that
I have some code below. This code is a basic push/pop stack class that
I basically have a program that filters records from one excel file to another
I basically have a unix process running and it is doing some heavy processing

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.