DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Computer Science (http://www.daniweb.com/forums/forum14.html)
-   -   "this" keyword in OOP (http://www.daniweb.com/forums/thread201644.html)

yakovm Jul 4th, 2009 8:28 am
"this" keyword in OOP
 
Hello
In many OOP languages "this"(or something similar) is used to point to current object. But i think its an object by itself. Here is what i found" polymorphic object which can point in different times to different object`s types"
I like to get some more information about this maybe some science article.
Thank you in advance.

Ancient Dragon Jul 4th, 2009 9:03 am
Re: "this" keyword in OOP
 
What more information could there possibly be??? As you already stated its just a pointer to the current c++ class object.

For example, here's the output of the following in c++. It shows that the this pointer is the same in both child and parent.
A:  this = 2afc38
B:  this = 2afc38
Press any key to continue . . .
class A
{
public:
    A()
    {
        x = 0;
    cout << "A:  this = " << hex << (unsigned int)this << "\n";
    }
private:
    int x;
};

class B : public A
{
public:
    B()
    {
        y = 0;
        cout << "B:  this = " << hex << (unsigned int)this << "\n";
    }
private:
    int y;
};


int main(int argc, char* argv[])
{
    B b;
}

csurfer Jul 4th, 2009 11:52 pm
Re: "this" keyword in OOP
 
Its just a Compilers way of saying "The main object under my concern right now is this" ,as AncientDragon has stated its just a pointer which is maintained completely by compiler which the user cannot modify,but can make use of it foir referencing the current object.For more info try these 1 2.

BestJewSinceJC Jul 7th, 2009 10:26 pm
Re: "this" keyword in OOP
 
In Java you can think of 'this' as being the same thing as the name of the Object. So if you are in the class MyClass, 'this' is your MyClass Object.


All times are GMT -4. The time now is 8:16 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC