Click to See Complete Forum and Search --> : what is "this", and why is "this" ? :)


Amn
November 21st, 2001, 06:29 AM
Hi All !

I noticed MFC guys use this pointer to store some data there (in CString). Is it a valid technique ? what does "this" point to , apart from the usual "it points to the object" stuff ? :)
is there some allocated memory referenced by it, we can use ?

I suppose data members are lined up at the pointer, and function addresses perhaps ? No i am talking crap right ? :)

Best wishes to the whole forum!

James Curran
November 21st, 2001, 07:41 AM
CString is a bit wacky. It's very efficient, it's just a bit strange.

There exists a struct (I don't know it's name), which hold the various details about a CString: Len, reference count, etc. It is a fixed size. Immedaitely after it in memory is the text of the string.

A CString object is just a pointer into the middle of that. It naturally points to the character date (so it works in printf statements), but if you substract a certain amount from the pointer, you get the other data.

So, any weirdness using "this" in CString is due to this bizare format. "this" has no meaning other than the usual "it points to the object" stuff.

Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

Amn
November 21st, 2001, 07:45 AM
but is it possible to allocate data at "this" location and store it safely ?

James Curran
November 21st, 2001, 08:01 AM
I'll need to see an example of what you are trying...


Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

Amn
November 21st, 2001, 09:08 AM
Sure :)

This is a simplified example, advanced one will only distract you from the point.


class CNumber
{

public:

void SetTo(int Number) { *this = Number; }
};




main:


CNumber A;

A.SetTo(55);

printf("%d", &A); /*should print 55 if 'this' has an allocation range of at least sizeof(int), or else an access violation should have occured :)*/




----------------------------------------------------------

I wonder what range does 'this' pointer have ? when i try to push big values inside a get an exception, but something like sizeof(int) or char[5] is OK ... i know it is bound to the size of the class itself, but that is as much as i know.

Amn.

James Curran
November 21st, 2001, 09:21 AM
*this

is a CNumber object, so *this = Number;

would call CNumber::operator=(int). in other words, that function could have been written as:void SetTo(int Number) { this->operator=(Number); }

or even just void SetTo(int Number) { operator=(Number); }



(I would probably full implement SetTo(), and call SetTo() in Oper=(), but doing the reverse is just as valid.)

Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.