May 01, 2024, 07:13:24 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Why this

Started by Vikki, November 29, 2006, 02:13:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Vikki

Ok, It's hard to search Google or anything else to learn about the 'this' keyword when you really don't know what you're looking for. And using the little highlight/right click trick doesn't work for 'this'.ÂÃ,  I mean it works but it brings up all the this's...not just the kind I want. Paul, can't you make the help file read my mind...eeek, maybe that's not such a good idea.ÂÃ,  :P

My question is why does 'this' work when used to set a parent and the name of the parent doesn't? For instance in the case of creating a control. In the docs it shows *parent. Lets say in the name of originality my parent name is myWindow. If I try to use *myWindow I get an error. If I try to use myWindow I get an error. But, I can use 'this' and not get an error?

Sorry, I'm trying really hard to get a handle on this...no pun intended.ÂÃ,  :o

Steven Picard

November 29, 2006, 02:30:46 PM #1 Last Edit: November 29, 2006, 02:33:48 PM by Steven Picard
The "this" keyword is a reference to the current class you are in.  For example, in the OnControl event for the kLearning object (as shown below) "this" is referencing the kLearning object itself.  So the function MessageBox wants a reference to a parent form and you are simply saying "this" (kLearning) is my parent form.

kLearning::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
    select(nID)
    {
        case 1:
            if(nNotifyCode = 0)
                Destroy();
        case 2:
            if(nNotifyCode = 0)
            {
                CControl *pControl = GetControl(4);
                MessageBox(this,"OK Pressed",pControl->GetText());
                SetFocus(4);
            }
    }
return 0;
}


Hopefully I didn't complicate that description.  I know Paul is far superior than I am at explaining things.

Steven Picard

Oh, and another thing.  Just as "this" only works when called from within a class you can't call an object by name from within itself.  If you called the MessageBox function from the "sub main" routine you would then use the name of the class (myWindow) but from within the class you would use "this" instead.

Vikki

Excellent!  ;D

'This' is not so mysterious as I was trying to make it to be and as scary as it is, I actually understood what you said.

Thanks a bunch Steven!!

And you're second answer explained why I was having trouble with something else. Two for the price of one...this is great!  8)

Ionic Wind Support Team

It is actually easy to undestand when you see what the compiler does to a method call.


CSomeclass c;
c.SomeMethod();

CSomeclass::SomeMethod()
{
     print( this );
}


Thats is what you type but what the compiler knows is that all methods have a hidden first parameter known as the 'this' pointer.  In essance a method is a function.  And the method above looks like this to the compiler:

sub CSomeclass@SomeMethod(CSomeclass *this)
{
    print(this);
}

So the entire code when the compiler parses it looks like this before it is translated to assembly:


CSomeclass c;
CSomeClass@SomeMethod(&c);

sub CSomeClass@SomeMethod(CSomeclass *this)
{
     print( this );
}


The 'this' pointer is a reference to the actual variable itself.  Which is how OOP really works.

Ionic Wind Support Team

Vikki