Results 1 to 11 of 11

Thread: This

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question This

    The keyword this represents within a class the address in memory of the object of that class that is being executed. It is a pointer whose value is always the address of the object.
    It can be used to check if a parameter passed to a member function of an object is the object itself.
    Taken from the cplusplus.com tutorial... I dont really understand what this sentence means...they give this example...i dont understand what's special about it...
    Code:
    //this
    #include <iostream.h>
    
    class CDummy
    {
    public:
    	int isitme(CDummy& param);
    };
    
    int CDummy::isitme(CDummy& param)
    {
    	if(&param == this) return 1;
    	else return 0;
    }
    
    void main()
    {
    	CDummy a;
    	CDummy *b = &a;
    
    	if(b->isitme(a))
    		cout <<"yep, &a is b\n";
    	else
    		cout << "nope, don't know this one\n";
    }
    i am not sure what the this pointer is...
    Thanks in advance!
    Amon Ra
    The Power of Learning.

  2. #2

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy just one thing...

    Code:
    //this
    #include <iostream.h>
    
    class CDummy
    {
    public:
    	int isitme(CDummy& param);
    };
    
    int CDummy::isitme(CDummy& param)
    {
    	if(&param == this) return 1;
    	else return 0;
    }
    
    void main()
    {
    	CDummy a;
    	CDummy *b = &a;
    
    	if(b->isitme(a))
    		cout <<"yep, &a is b\n";
    	else
    		cout << "nope, don't know this one\n";
    }
    What I dont get here, is what does the this pointer contain when i call the function: the address of *b or a?
    Amon Ra
    The Power of Learning.

  3. #3

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    oups

    i dont knoe why, but the &param shows up as¶m =)
    Amon Ra
    The Power of Learning.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hmm bizarre thing there

    Anyway, this is a pointer to the instance of that class. What you're doing with the second one is you're passing a pointer rather than a class, so it shouldn't even compile
    Code:
    #include <iostream>
    
    using namespace std; // Just making you standards-compliant :)
    
    class CDummy {
    public:
        int isitme(CDummy *param); // Using pointers instead, it's easier
    };
    
    int CDummy::isitme(CDummy *param) {
        if(param == this) return 1;
        return 0;
    }
    
    int main() {
        CDummy a;
        CDummy *b = &a;
    
        if(b->isitme(&a))
            cout <<"yep, &a is b\n";
        else
            cout << "nope, don't know this one\n";
    
        return 0;
    }
    That should print "yep, &a is b"
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    he probably typed &param but it got &param
    param is a reference, didn't you notice parksie? it should compile
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    CDummy a;
    CDummy *b = &a;
    
    if(b->isitme(a))
    It's expecting a reference not a pointer Wouldn't passing a pointer violate the rules of referencing?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Eh, well a is not a pointer right? if b is null (for some odd reason), it still fires isitme and pass a right?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ok I can't read...I thought b was a
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hehehe
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I really need to get a good monitor
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Smile

    Thanks you very much guys!!
    Amon Ra
    The Power of Learning.

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