CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    what is "this", and why is "this" ? :)

    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!


  2. #2
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: what is "this", and why is "this" ? :)

    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.

  3. #3
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Re: what is "this", and why is "this" ? :)

    but is it possible to allocate data at "this" location and store it safely ?


  4. #4
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: what is "this", and why is "this" ? :)

    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.

  5. #5
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Here is an example

    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.


  6. #6
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Here is an example

    *this

    is a CNumber object, so *this = Number;

    would call CNumber:perator=(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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured