walkinginwater
December 3rd, 2004, 11:14 AM
I have 4 question about the operator ->* overloading:
first:The operator–>* is a binary operator
but when we define the implementation of it we just take one argument, i don't know why , can somebody give me an explanation?
Second:From the following example, i have no idea about the usefulness of ->*overloading,it is too troublesome to create the FunctionObject class. Can we just forbidden the overloading of it??
third:why the statement "w->pmf*" can work?
the output of this is : operator->*
FunctionObject constructor
Fourth:why we cannot use the statement: w->*pmf(12);
#include <iostream>
using namespace std;
class Dog
{
public:
int run(int i) const
{ cout << "run\n";
return i;
}
typedef int (Dog::*PMF)(int) const;//define the pointer to the member function
class FunctionObject // operator->* must return an object that has an operator(),
{ //so we should first create a class to store the information
private: // that we need to implement the () function
Dog* ptr; PMF pmem;
public:
FunctionObject(Dog* wp, PMF pmf) : ptr(wp), pmem(pmf)
{ cout << "FunctionObject constructor\n"; }
int operator()(int i) const
{ cout << "FunctionObject::operator()\n";
return (ptr->*pmem)(i); // Make the call
}
};
//the definition of the operator->* overloading
FunctionObject operator->*(PMF pmf)
{ cout << "operator->*" << endl;
return FunctionObject(this, pmf);
}
};
int main()
{ Dog w;
Dog::PMF pmf = &Dog::run;
cout << (w->*pmf)(1) << endl;
w->pmf*;//why this can work
//w->pmf*(12) //error C2064: term does not evaluate to a function taking 1 arguments
} ///:~
first:The operator–>* is a binary operator
but when we define the implementation of it we just take one argument, i don't know why , can somebody give me an explanation?
Second:From the following example, i have no idea about the usefulness of ->*overloading,it is too troublesome to create the FunctionObject class. Can we just forbidden the overloading of it??
third:why the statement "w->pmf*" can work?
the output of this is : operator->*
FunctionObject constructor
Fourth:why we cannot use the statement: w->*pmf(12);
#include <iostream>
using namespace std;
class Dog
{
public:
int run(int i) const
{ cout << "run\n";
return i;
}
typedef int (Dog::*PMF)(int) const;//define the pointer to the member function
class FunctionObject // operator->* must return an object that has an operator(),
{ //so we should first create a class to store the information
private: // that we need to implement the () function
Dog* ptr; PMF pmem;
public:
FunctionObject(Dog* wp, PMF pmf) : ptr(wp), pmem(pmf)
{ cout << "FunctionObject constructor\n"; }
int operator()(int i) const
{ cout << "FunctionObject::operator()\n";
return (ptr->*pmem)(i); // Make the call
}
};
//the definition of the operator->* overloading
FunctionObject operator->*(PMF pmf)
{ cout << "operator->*" << endl;
return FunctionObject(this, pmf);
}
};
int main()
{ Dog w;
Dog::PMF pmf = &Dog::run;
cout << (w->*pmf)(1) << endl;
w->pmf*;//why this can work
//w->pmf*(12) //error C2064: term does not evaluate to a function taking 1 arguments
} ///:~