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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:07:11+00:00 2026-06-01T07:07:11+00:00

I’m currently trying to grasp both function overloading and function pointers. In order to

  • 0

I’m currently trying to grasp both function overloading and function pointers.
In order to shorten a piece of code I want/need to make a function pointer to a comparison operator. In my original code i loop trough and compare lots of pairs of float variables.
My actions after the comparison is dependent on wether a third semi-static variable is positive or negative. In this version i either have to check the value of the semi-static variable for every pair or I have to replicate a lot of code.

double angleRight; //This variable is either positive or negative and is not reassigned for the purpose of this code

while (points.size() > 2){
siz = points.size();
for (int i = 0; i < siz; i++){
  if (angleRight > 0 && points[i].angle < 0){
<Do something>
    <remove points[i]>
  } else if (angleRight < 0 && points[i].angle > 0){
  <Do something else>
      <remove points[i]>
  }
}

If I instead could evaluate angleRight once and then store a function-pointer to either operator> or operator<, I would be able to use this function-pointer instead and could avoid evaluating angleRight as well as the entire ‘else’-block.
I have tried to understand function pointers and (I think) I see how I could have managed if i wanted access to an overloaded member-function.

//This compiles
class Bs{
  public:
  float x;
  bool operator< (Bs y){
    return x < y.x;
  }
};
bool (Bs::*compare) (Bs) /*const*/ = &Bs::operator<;

But what i really want to do/imagine is something like this:

//This does not compile:
bool (*compar) (float) /*const*/ = &float::operator<;

EDIT:
Making the two functions ‘greater’ and ‘less’ does what I want:

bool greater(float x, float y){
  return x > y;
}
bool less(float x, float y){
  return x < y;
}

bool (*compar) (float, float) = (angleRight < 0)? &greater : &less;

But it annoys me that I actually have to write the functions. Is there no way of directly accessing the float-operator> ?

  • 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-01T07:07:13+00:00Added an answer on June 1, 2026 at 7:07 am

    The C++ way of doing things like this is not by accepting a function pointer, but by accepting a function-like object.

    An example:

    template <class T, class Cmp> int cmp(T x, T y, Cmp cmp_func = std::less) {
        return cmp_func(x, y) - cmp_func(y, x);
    }
    

    In this example we don’t care exactly what Cmp is, as long as it supports operator() with two operands.

    If your function accepts a function-like-object you can use standard objects like std::less automatically too solving this question.

    • 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
I want use html5's new tag to play a wav file (currently only supported
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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.