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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:23:31+00:00 2026-05-25T14:23:31+00:00

Consider I have a hierarchy defined as below class Strategy { public: virtual void

  • 0

Consider I have a hierarchy defined as below

class Strategy
{
public:
    virtual void Run();
};
class StrategyA : public Strategy
{
public:
    virtual void Run();
};
class StrategyB : public Strategy
{
public:
    virtual void Run();
};

I was wondering if I replace the Run() with operator() makes any sense and if there are any advantages from a design and efficiency perspective.

class Strategy
{
public:
    virtual void operator()();
};
class StrategyA : public Strategy
{
public:
    virtual void operator()();
};
class StrategyB : public Strategy
{
public:
    virtual void operator()();
};

Thanks

CV.

  • 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-05-25T14:23:32+00:00Added an answer on May 25, 2026 at 2:23 pm

    Yes. Its fully makes sense.

    Any operator overload is a function, after all. It adds syntactic sugar to the language. Sometimes, they’re necessary, but often, it’s just syntactic sugar.

    Note that you’ve to invoke it polymorphically (of course, if you want runtime-polymorphism), and there’re two ways you can do that:

    • using pointer of base type, and
    • using reference of base type

    Example (demo),

    struct A
    {
       virtual void operator()() { cout << "A" << endl; }
    };
    struct B : A
    {
       virtual void operator()() { cout << "B" << endl; }
    };
    
    int main() {
            B b;
    
            //using pointer
            A *ptr = &b;
            (*ptr)(); //clumsy!  - prints B
    
            //using reference
            A &ref = b;
            ref();   //better    - prints B
    
            //also correct
            b();  //prints B
            return 0;
    }
    

    And if you’ve a function template written as:

    template<typename Functor>
    void call(Functor fun)
    {
       (*fun)();
    }
    

    Then you can use this function, for functors and regular functions, both:

    void regular_function()
    {
       cout << "regular_function" << endl;
    }
    
    B b;
    call(&b);  //prints B
    call(regular_function); //prints regular_function
    

    Demo : http://ideone.com/B9w16

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

Sidebar

Related Questions

Consider the following class hierarchy: public abstract class Entity { public virtual int Id
Consider I have the following interface: public interface A { public void b(); }
Consider you have the following class public class OuterClass { ... private static class
Consider I have two classes. Professor and TimePerDay. public class TimePerDay { private ObservableCollection<TimeSpan>
Consider I have 2 classes: public class ComplicatedParent { // Lots of data }
Consider I have a these two properties: public class Test { [Required(ErrorMessage = Please
Let's have the following class hierarchy: public class MyType { } public class MySuperclass<T
Consider I have a shared function:- Public Shared Function CalculateAreaFromRadius(ByVal radius As Double) As
Consider I have an anchor which looks like this <div class=res> <a href=~/Resumes/Resumes1271354404687.docx> ~/Resumes/Resumes1271354404687.docx
Consider I have a DataContext db , and there is an Entity class User

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.