Let me rewrite the operator for you:
1 2 3 4
|
int test::operator()(int foo)
{
return foo;
};
|
What did change? Nothing. The two programs are identical.
The operator has an argument. In your code the argument has name 'a'.
The argument is an integer variable.
Your class has member variable. Also an integer and also with name 'a'.
The two are not the same. In the body of the function the name 'a' refers to the variable that has been declared in inner scope. The variable of outer scope is
masked. Hidden.
The member can still be explicitly referred to via the
this
pointer:
1 2 3 4 5
|
void test::bar( int a )
{
std::cout << a; // function's argument
std::cout << this->a; // member of class
};
|
You should avoid identical names, where they cause confusion.
The operator does not touch
this->a
. It merely returns its argument.
That said, your output statement is in the
constructor
.
The output occurs as part of evaluating line 24.
Even if your operator() would do something, it would be too late.