Thread: this pointer?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    33

    this pointer?

    hello,

    I have two questions:

    1.What is the function of this pointer?

    2.Where it is used?Can we avoid to use it?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    What does your C++ book say about it?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by salmanriaz View Post
    1.What is the function of this pointer?
    when you have a class member function, you can use the 'this' pointer from inside that function to reference the object.

    2.Where it is used?Can we avoid to use it?
    let's say your class has a member function called gets(). there is also a standard C library function called gets() and in order to disambiguate which one you're calling, you could say this->gets(). this may be more for the benefit of the person who must maintain the code, but there are situations where it is necessary to avoid compiler warnings and errors.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I'm a slowpoke but:

    the *this-pointer inside a class is good for many things, for example if you got a class-function where you have a parameter with the same name as a class-member, when you want to call a member-function that has the same name as a global one, or when you need to return the class by reference from a function to allow for "chain-function-calls":

    dirty example:
    Code:
    #include <iostream>
    
    class MyClass {
    	public:
    		MyClass (int a);
    		MyClass& inc (void);
    		MyClass& print (void);
    
    	private:
    		int a;
    };
    
    MyClass::MyClass (int a) 
    {
    	this->a = a;
    }
    
    MyClass &
    MyClass::inc (void)
    {
    	(this->a)++;
    
    	return *this;
    }
    
    MyClass &
    MyClass::print (void) 
    {
    	std::cout << this->a << std::endl;
    
    	return *this;
    }
    
    int
    main (int argc, char *argv[])
    {
    	MyClass x (5);
    	x.inc().inc().inc().inc().print(); /* "chain-function-calls" */
    
    	return 0;
    }
    ("this->member" is also makes it more clear what variable you intend to be changing/using, at least to me)
    Last edited by edoceo; 03-23-2009 at 06:52 AM. Reason: I'm slow.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is why it's a good reason to name the member variables with a prefix, such as m_.
    And empty functions (ie functions taking no arguments) don't need void in the declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    This is why it's a good reason to name the member variables with a prefix, such as m_.
    I would argue that relying on a naming convention to denote type or scope is fraught with peril. Kind of like relying on comments to be accurate.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    I would argue that relying on a naming convention to denote type or scope is fraught with peril. Kind of like relying on comments to be accurate.
    I'd rather type 2 characters "m_" rather than 6 characters "this->".
    Not everyone uses this-> all the time, and then the variable looks like a regular function variable instead of a member variable.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    I would argue that relying on a naming convention to denote type or scope is fraught with peril. Kind of like relying on comments to be accurate.
    How do you handle this?

    Code:
    class foo
    {
    public:
        void setValue(int value)
        {
            value = value; // ???
        }
    
    private:
        int value;
    };
    The argument cannot have the same name as the member. So you have to either apply a name decoration to the argument, or to the member. You have no choice. Otherwise you end up using "this->" which is what we're trying to avoid.

    I don't like Hungarian notation, but applying a special prefix or suffix to member variables seems appropriate.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Like this?

    Code:
    class foo
    {
    public:
        void setValue(int new_value)
        {
            value = new_value; // ???
        }
    
    private:
        int value;
    };
    However, there is no point going into a flame war over naming conventions.

    The this pointer is simply how you refer to the current instance and it has much more meaningful uses than letting you use the same variable name in the same scope.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    How do you handle this?

    Code:
    class foo
    {
    public:
        void setValue(int value)
        {
            value = value; // ???
        }
    
    private:
        int value;
    };
    The argument cannot have the same name as the member. So you have to either apply a name decoration to the argument, or to the member. You have no choice. Otherwise you end up using "this->" which is what we're trying to avoid.

    I don't like Hungarian notation, but applying a special prefix or suffix to member variables seems appropriate.
    Name the parm something other than "value"?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anon
    Like this?
    Quote Originally Posted by medievalelks
    Name the parm something other than "value"?
    brewbuck already covered that by stating "apply a name decoration to the argument".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Is this a name decoration?

    Code:
    void setValue(int val)
    {
        value = val;
    }

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    Is this a name decoration?

    Code:
    void setValue(int val)
    {
        value = val;
    }
    My point is, why do you accept having to change the argument name but don't accept changing the member name?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    My point is, why do you accept having to change the argument name but don't accept changing the member name?
    For one, I rarely see consistency in how these are applied in any moderately sized project with more than one developer. Second, if the variable changes later to become, say, a file scope static variable, the name must change or it becomes misleading.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm not changing the argument name, I just give it a different name? Would you go out of your way to use the same name for different variables in different scopes? E.g do you feel tempted to use i as the counter for all nested for-loops?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM