overloading operator() in c++
-
Can anyone help me find what's wrong with operator() overload pls? #include using namespace std; class A { public: int value; A(int a) : value(a) {} operator int() { return value; } void operator ++ () // prefix { value = value+1; } }; int main() { A a1(27); ++a1; // this works fine... // the following does not...MSVC6 tells me "error C2064: term does not evaluate to a function" cout << "value:" << a1() << endl; return 0; } Any ideas?! Many thanks...! Chris
-
Can anyone help me find what's wrong with operator() overload pls? #include using namespace std; class A { public: int value; A(int a) : value(a) {} operator int() { return value; } void operator ++ () // prefix { value = value+1; } }; int main() { A a1(27); ++a1; // this works fine... // the following does not...MSVC6 tells me "error C2064: term does not evaluate to a function" cout << "value:" << a1() << endl; return 0; } Any ideas?! Many thanks...! Chris
yeah there is one idea ... try this cout<<"value:"<< a1.operator int()<< endl ; > in place of ur line cout << "value:" << a1() << endl; :) actually u have overloaded int() operator :) Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)
-
yeah there is one idea ... try this cout<<"value:"<< a1.operator int()<< endl ; > in place of ur line cout << "value:" << a1() << endl; :) actually u have overloaded int() operator :) Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)
-
I see...but how do I overload the () operator if i need to use it like this in my code: cout << "value: " << a1() << endl; Chris
operator()()
Lets be honest, isn't it amazing how many truly stupid people you meet during the course of the day. Carry around a pad and pencil, you'll have twenty or thirty names by the end of the day - George Carlin Awasu 1.2 [^]: A free RSS reader with support for Code Project.
-
I see...but how do I overload the () operator if i need to use it like this in my code: cout << "value: " << a1() << endl; Chris
for that purpose overload your () operator like this // overloading () operator int operator()() { return value; } now u can access that bracket operator like a1() i hope ur prob is solved now :) regards Abhishek Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)
-
for that purpose overload your () operator like this // overloading () operator int operator()() { return value; } now u can access that bracket operator like a1() i hope ur prob is solved now :) regards Abhishek Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)
-
operator()()
Lets be honest, isn't it amazing how many truly stupid people you meet during the course of the day. Carry around a pad and pencil, you'll have twenty or thirty names by the end of the day - George Carlin Awasu 1.2 [^]: A free RSS reader with support for Code Project.
-
Can anyone help me find what's wrong with operator() overload pls? #include using namespace std; class A { public: int value; A(int a) : value(a) {} operator int() { return value; } void operator ++ () // prefix { value = value+1; } }; int main() { A a1(27); ++a1; // this works fine... // the following does not...MSVC6 tells me "error C2064: term does not evaluate to a function" cout << "value:" << a1() << endl; return 0; } Any ideas?! Many thanks...! Chris
Christopher Spiteri wrote:_
class A
{
public:
int value;
// ...
operator int()
{
return value;
}
// ...
};_Since there's only one value type operator, int, in your class A, cout will take it implicitly. So you can write it like this:
cout << "value:" << a1 << endl;
And, operator int() is for something like this:
cout << "value:" << (int)a1 << endl;
Overloading () would be this way:
A& operator ()()
{
cout << "In operator ()\n";
return *this;
}Therefore, in main, you can write this:
int main()
{
A a1(27);
++a1; // this works fine...a1(); // This line. return 0;
}
Maxwell Chen
-
Christopher Spiteri wrote:_
class A
{
public:
int value;
// ...
operator int()
{
return value;
}
// ...
};_Since there's only one value type operator, int, in your class A, cout will take it implicitly. So you can write it like this:
cout << "value:" << a1 << endl;
And, operator int() is for something like this:
cout << "value:" << (int)a1 << endl;
Overloading () would be this way:
A& operator ()()
{
cout << "In operator ()\n";
return *this;
}Therefore, in main, you can write this:
int main()
{
A a1(27);
++a1; // this works fine...a1(); // This line. return 0;
}
Maxwell Chen