Michael Bell wrote:
In message <fede62$k2$1@news.dtag.de>
Stuart Redmann <DerTopper@web.de> wrote:
Michael Bell wrote:
I am using "C++ programming in easy steps" by Mike McGrath,
ISBN 1-84078-295-1. It doesn't give much explanation, it seems to
think you can work it out for yourself, which mostly I can. The
explanation of "this" is only 2 lines long, and I can't follow it.
[snipped my previous response where I urged the OP to elaborate further]
I think I have made some progress using McGrath, it may be an
irrelevant feature, but it uses THIS a lot. MT262 only mentions it
once, and that as an aside. In another forum, somebody wrote that
"THIS is CENTRAL to C++". Maybe, like many features of maths or
natural languages, you can do the same thing in many different ways
and it's a matter of personal style which way you do it.
McGrath uses THIS a lot, and explains it thus:
"Acessor methods must be defined, as usual. Each object has a special
point named THIS which refers to the object itself. So, object members
can be referred to as THIS -> age, etc. This can be useful in the
accessor method definitions where the argument may often have the
same name as the class member [but isn't it good practice not to do
this?] The THIS pointer can distinguish between the argument and the
class member."
Let's go into some implementation detail. If you have plain functions,
everything is quite simple: every bit of data that should be
influenced should
be passed as parameter to the function.
If we are talking about objects and methods, you'll probably have
asked yourself
how methods are actually implemented by the compiler. The answer is that they
are implemented just like simple function calls but differ in one
respect: As a
method influences the internal state of a particular instance/object (for
example Fido.setAge), the functional implementation needs access to
the memory
of the object (in that case the implementation needs to know where the memory
for Fido is located). Thus the functional implementation of methods have a
standard parameter that is the pointer to the object that should be
manipulated.
Voila, there you have the 'this' pointer.
If you compile your project with Visual C, and you forget to provide the
implementation of Dog::setAge (int age), the linker will complain about this
missing implementation with the following error message:
error LNK2001: Unresolved external symbols: "public: void __thiscall
Dog::SetAge(int)" (?SetAge@Dog@@QAEXH@Z)
Have a close look at what is written in parenthesis, this is the name of the
internal functional implementation (if you wanted to provide an
implementation
of this method with a C compiler, you would have to give this function
exactly
this name). As the MS compiler uses name mangling (the name of the
function gets
the type of its parameters attached), one can deduct that the internal
functional implementation of Dog::SetAge takes _two_ parameters
instead of one:
?SetAge@Dog@@QAEXH@Z
--- - "H" means int parameter.
|--- "Dog" means pointer to Dog object.
McGrath uses THIS like to put data in like this:-
Dog Fido
Fido.setAge(3);
.
.
void Dog::setAge(int age)
{
THIS -> age = age;
}
This is a matter of style. I never use the 'this' pointer for this
purpose as I
adopt the Hungarian notation.
Regards,
Stuart