"Newsgroup - Ann" <ne******@yahoo.com> wrote in message
news:3f**********@rcfnews.cs.umass.edu
To see why you have to use this->*callee rather than just callee,
suppose you have two objects, a1 and a2. Then each object might
store
a pointer to the other A object, called otherAObjectPtr. In that
case, you would be allowed to call
otherAObjectPtr->*callee
so that the function would use the "this" pointer (and hence the
member data) from the other object rather than its own. It is
because
of that flexibility that you need to be explicit that you want to
use
the "this" pointer of the current object.
but the 'this' for the reference to its own _member data_ could be
omitted, why not the same for the _member function_?
[Re-post after the first seemed to disappear]
Pointers to member functions have a rather complicated character. For
comparison, think about the following members:
1. Ordinary data members, e.g., an int called m.
2. Member functions, e.g., a function called foo
When you use m within a class, it is implicitly replaced by this->m. The
role of "this" is to identify the object to which m belongs.
When you use foo within a class, it it implicitly replaced by this->foo. The
role of "this" is *not* to identify the object to which foo belongs, because
foo is the same for all objects in the class and does not belong to
individual objects. Rather, "this" identifies the object that foo is to use
if it needs to access member data.
Now consider a pointer to a member function that is itself a data member of
the A class. Suppose that there are two objects, a1 and a2, and each stores
a pointer to a member function, called ptr. Suppose that the ptr value
stored by a1 points to A::foo(), whereas the ptr value stored by a2 points
to A::goo().
Now if an A object invokes ptr, there are two bindings to be made, not one.
These correspond to the two sorts of bindings discussed above for m and foo.
First, it must be decided which ptr value to use --- the ptr value stored in
a1, pointing to the function A::foo(), or the ptr value stored by a2,
pointing to the function A::goo(). Second, in either case, it must be
decided which member variables the member function should use, those from a1
or those from a2, e.g., should it use the m integer from a1 or the m integer
from a2. It is possible to mix and match, e.g., you could use the ptr value
stored in a2 (giving the function A::goo() ), yet have the member function
pointed to use the member data of a1.
If, within the scope of the A class, you type:
(this->*ptr)();
then you are indeed being forced to make one "this" explicit. But there is a
second "this" that is being supplied implicitly, just like it normally is.
What you type is implicitly converted to:
(this->*this->ptr)();
The explicit "this" on the left shows that you want the function to use the
member variables of the current object. The implicitly supplied "this" on
the right shows that you want to use the ptr value of the current object.
The general syntax is
(pObjectSupplyingMemberVariables->*pObjectToWhichPointerBelongs->ptr)();
The rules concerning when "this" must be explicit and when "this" is
supplied implicitly mean that the ptr variable is being treated like the m
int variable but not like the foo() function --- which makes sense because
ptr is a variable like m, not a function like foo.
Now, you might prefer that both "this" pointers were supplied implicitly. I
don't know what, if any, technical difficulties that might pose for compiler
writers. But I hope that I have persuaded you that there is an underlying
consistency in the current syntax.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)