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.
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:
Example (demo),
And if you’ve a function template written as:
Then you can use this function, for functors and regular functions, both:
Demo : http://ideone.com/B9w16