Click to See Complete Forum and Search --> : Where are "this" pointers are assigned ?


Lacura
June 18th, 2002, 09:30 PM
As we all know, in C++, this means a pointer to
the object itself. But where and when is the pointer
created?

I have try the follwing cod:

class A{
A(){ cout<<"A::A() is called."; }
A(int a){ A(); cout<<"A:A(int a) is called"; };
~A(){ cout<<"A is destructing ...";}
}

main(){
A a;
return 0;
}

it shows that in the constructor A::A(int),
another tempory object of A is created.
Then the question is rasied:
Where and when is this be assigned,
at the beginning or at the end of the constructor?

Alexis Moshinsky
June 18th, 2002, 11:49 PM
Hi Lacura,

'this' is not created and not assigned.
Every time You instantiate class, first thing is being done -allocating memory block.
Then start address of the memory block is implicitly passed to every non-static
member function (to constructor like to every other n. static member function)

So, 'this' is the keyword, associated with hidden parameter.