How does the compiler handle overloaded functions that invoke operators in functors?

Let's say I define, instantiate, and use the adder functor as follows:

class SomeAdder {
    public:
        SomeAdder(int init_x): x(init_x) {}
        void operator()(int num) { cout << x + num <<endl; }
    private:
        int x;
};

SomeAdder a = SomeAdder (3);
a(5); //Prints 8

SomeAdder b(5);
b(5); //Prints 10

The constructor and the overloaded operator ()are called using double brackets and have the same parameter types. How will the compiler determine which function to use during instances SomeAdderand "function calls" to implement the correct behavior? The answer seems to be obvious on the surface, but I just can't plunge into this thought.

Thank you for your time!

+5
source share
3 answers

-, operator(). , . :

  • , .

  • - . - operator().

, . , .

+4

, , . . , operator () .

+1

++ , ( ), , , ().

As grammar is used to determine this, a compiler course is probably required, which is possibly the Dragon Book standard . If you're interested, you can also check out C ++ Grandmaster Certification , which aims to create a C ++ compiler.

+1
source

All Articles