Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 01-30-2005, 02:22 PM   #1 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Explicit vs. Implicit use of the this Pointer

I have a few questions (Valmont) about using the this Pointer. I'm trying to understand more about how it's working under the hood. Let me start with some code:

Example class and main()
Code:
#include <iostream>
using std::cout;

class Example
{
    public:
        Example(int = 0);
        void print() const;

    private:
        int data;
};

Example::Example(int x)
    : data(x)
{}

void Example::print() const
{
    // Implicitly use this pointer
    cout << "data       = " << data << "\n";

    // Explicitly use this pointer
    cout << "this->data = " << this->data << "\n";
}

int main()
{
    Example obj_one(150);
    obj_one.print();

    return 0;
}


And the program output...
Code:
data       = 150
this->data = 150
To me it seems like implicit vs. explicit use of the this pointer is actually the exact same under the hood. Right now I'm under the assumption that the compiler would be converting implicit calls to explicit calls at the time of compilation, can you tell me if this is correct?

Also if anyone wants to add anything that might aid in my understanding of this that would also be appreciated.
fp_unit is offline   Reply With Quote
Old 01-30-2005, 03:19 PM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Although I am not Valmont (nor will I ever be) and not as talented as him in coding, I believe your assumption to be correct. this is merely a pointer to the current object. It is an implicitly passed parameter to every member function of a class. Its main use is in copy constructors and overloaded operators.

Hope this helps and I havn't butchered this too bad for all the experts out there.

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 01-30-2005, 04:20 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
That's basically correct. However, using the "this" pointer explicitly is required when using pointers-to-members-functons.
The typical difference between a function pointer and a member pointer in a nutshell:
the pointer to member *has* to have an object calling it.

Therefore in this case using the "this" keyword is required.

fp_unit, there is a short tutorial on the "this" keyword in tbe C++ tutorial section.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What could I use a pointer var for? slashdot Standard C, C++ 8 01-31-2005 06:33 AM
array pointer? Admin PHP 8 12-08-2003 04:51 PM
Java help?? d00ds?? zergmuncher Java 19 11-15-2003 03:35 AM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 06:12 AM


All times are GMT -8. The time now is 04:13 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting