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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:23:53+00:00 2026-06-02T11:23:53+00:00

I would like to have a class implement operator() several different ways based on

  • 0

I would like to have a class implement operator() several different ways based on an option set in the class. Because this will be called a large number of times, I don’t want to use anything that branches. Ideally, the operator() would be a function pointer that can be set with a method. However, I’m not sure what this would actually look like. I tried:

#include <iostream>

class Test {
public:
  int (*operator())();

  int DoIt1() {
    return 1;
  }

  int DoIt2() {
    return 2;
  }

  void SetIt(int i) {
    if(i == 1) {
      operator() = &Test::DoIt1;
    } else {
      operator() = &Test::DoIt2;
    }
  }
};

int main()
{
  Test t1;

  t1.SetIt(1);

  std::cout << t1() << std::endl;

  t1.SetIt(2);

  std::cout << t1() << std::endl;

  return 0;
}

I know it will work if I create another function pointer and call that from the operator() function. But is it possible to have the operator() function itself be a function pointer? Something along the lines of what I posted (which doesn’t compile)?

The above code gives:

test.cxx:5:21: error: declaration of ‘operator()’ as non-function

test.cxx: In member function ‘void Test::SetIt(int)’:

test.cxx:17:16: error: ‘operator()’ not defined

test.cxx:19:16: error: ‘operator()’ not defined

test.cxx: In function ‘int main()’:

test.cxx:30:19: error: no match for call to ‘(Test) ()’

test.cxx:34:19: error: no match for call to ‘(Test) ()’

  • 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-02T11:23:55+00:00Added an answer on June 2, 2026 at 11:23 am

    Your class needs to somehow remember what function pointer to use. Store it as a class member:

    class Test
    { 
    public:
        Test() : func(0) {}
    
        int operator()() {
            // Note that pointers to Test member functions need a pointer to Test to work.
            return (this->*func)(); // undefined behavior if func == 0
        }
    
        void SetIt(int i) { 
            if(i == 1) { 
                func = &Test::DoIt1; 
            } else { 
                func = &Test::DoIt2; 
            } 
        }
    
    private:
        int DoIt1() { 
            return 1; 
        } 
    
        int DoIt2() { 
            return 2; 
        } 
    
        // Typedef of a pointer to a class method.
        typedef int (Test::*FuncPtr)(); 
        FuncPtr func; 
    };
    

    However, before you go into the effort of doing this, profile your code first and see if branching via switch or if is actually a bottleneck (it might not be!). Modern processors have very counterintuitive performance characteristics, so compilers may be able to generate better code than you think it does. The only way to make sure that branching is actually too costly for you to use is to profile your code. (And by “profiling” I mean “run well-designed experiments”, not “come up with a hunch without testing”.)

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

Sidebar

Related Questions

I have a class that is called regularly by several objects. I would like
Supposed I have a class called TrafficLight and I would like to have a
I have a small class hierarchy where I would like to have a child
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I would like to have a private property in a class, and be able
I would like to have a datacontext available in every controller: public abstract class
I have a class in silverlight that I would like to store to disk.
I have a class that is marked with DataContract attributes and I would like
I have a class MyClass, and I would like to override the method ToString()
I have a class in my asp.net proj, I would like to get access

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.