this pointer

use of this pointer

Page 1 of 1

9 Replies - 2150 Views - Last Post: 19 April 2006 - 05:18 AM Rate Topic: -----

#1 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

this pointer

Posted 15 April 2006 - 07:41 AM

'this pointer' refers to the address of the object calling the member function.here is a program using it to make merit list

//TO DEFINE A CLASS THAT REPRESENTS A STUDENT AND HAS A FUNCTION TO TAKE HIS
//MARKS AND CALCULATE HIS PERCENTAGE.THEN PREPARE AN MERIT LIST

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int pos;

class student
{
        public:

        char name[30];
        int rno;                     //rno=>the roll no. of the student
        float a,b,c,d,e,per;

        

        void input()
        {
                cout<<"\n ENTER THE NAME OF THE STUDENT :";
                gets(name);
                cout<<"\n ENTER THE ROLL NO. OF THE STUDENT :";
                cin>>rno;

                cout<<"\n ENTER THE DETAILS OF THE MARKS OF THE STUDENTS :"
                      "\n PHYSICS :";
                cin>>a;
                cout<<"\n CHEMISTRY :";
                cin>>b;
                cout<<"\n MATHS :";
                cin>>c;
                cout<<"\n ENGLISH :";
                cin>>d;
                cout<<"\n COMPUTER :";
                cin>>e;
        }

        void output()
        {
                cout<<"\n THE NAME OF THE STUDENT :";
                puts(name);
                cout<<"\n THE ROLL NO. OF THE STUDENT :";
                cout<<rno;

                cout<<"\n THE DETAILS OF THE MARKS OF THE STUDENTS :"
                      "\n PHYSICS :";
                cout<<a;
                cout<<"\n CHEMISTRY :";
                cout<<b;
                cout<<"\n MATHS :";
                cout<<c;
                cout<<"\n ENGLISH :";
                cout<<d;
                cout<<"\n COMPUTER :";
                cout<<e;
        }

        void calculate()
        {
       per=(a+b+c+d+e)/5;
        }

        student compare(student obj,int &j)
        {
       if(per<obj.per)
         {
    pos=j;
    j++;
    return obj;
         }
       else
   return *this;

       }
};

void sort(student ar[],int size)   //sorts the array in order of increasing percentage
{

        student large,temp;

        for(int i=0;i<size;i++)
        {
   large=ar[i];
            //find the highest scorer
   for(int j=i+1;j<size;j++)
       {
        large=ar[j].compare(large,j);

        }

   temp=ar[i];
   ar[i]=ar[pos];
   ar[pos]=temp;
         }


}

void main()
{
        clrscr();

        int size;
        cout<<"\n ENTER THE NO. OF STUDENTS WHOSE RECORDS ARE TO BE ENTERED :";
        cin>>size;

        student *ar= new student[size];   //dynamic intialization of array


        for(int i=0;i<size;i++)
   {
       clrscr();
       ar[i].input();
       getch();
   }

        sort(ar,size);

        cout<<"\n THE MERIT LIST IS :";
        getch();
        for( i=0;i<size;i++)
   {
       clrscr();
       ar[i].output();
       getch();
   }



}





Is This A Good Question/Topic? 0
  • +

Replies To: this pointer

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: this pointer

Posted 15 April 2006 - 09:34 AM

good job, your example (when you are returning the this pointer) is about the only situation where this is even needed. Operations excluding the word this, will assume it is true unless another identifier is used.
Was This Post Helpful? 0
  • +
  • -

#3 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: this pointer

Posted 15 April 2006 - 11:28 AM

'this' is already a pointer you dont need the * before it.

and the only situation you'd need to use 'this' pointer is when you want to return address to the object. A really good example would be when you are overloading the =, +, -, etc.. signs.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: this pointer

Posted 15 April 2006 - 05:47 PM

*Just a note, a * BEFORE an a poiner does not declare it as a pointer, but rather is a derefrencing operator (which can also be overloaded), and returns the object that this is pointing to, eg:
this->someObject
then:
*this would return someObject and not a pointer to some Object.

Since the example above is returning a student object in the method declaration, *this is correct, if it was returning student* then returning a straight this would be correct, but then this would need to be handled in the main by either a -> (points too operator) or by derefrencing the student object with the prefix * operator. :)
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: this pointer

Posted 16 April 2006 - 01:16 AM

Yes, Good Point.
As Mrafcho001 said, Overloading operators is a much better idea.
Instead of saying student[i].input(); you can even overload the >> operator and use cin for input.
Was This Post Helpful? 0
  • +
  • -

#6 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: this pointer

Posted 16 April 2006 - 02:59 AM

the return data type for the function is student. *this refers to the value at this .i havent redeclared it as a pointer. i used a function to get the input i didnt see . i was gonna declrare them as private.but i think i could hav used a structure too.they also have a this pointer right? :sleepy:
Was This Post Helpful? 0
  • +
  • -

#7 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: this pointer

Posted 16 April 2006 - 03:30 PM

theoretically yes, but structures classically do not have methods within them, any class, member function or object has a this pointer (being the class which has invoked the code being run)

The easiest example i would think (outside of understanding the overloader) is the = overloader:
anObject& operator=(const anObject o){
  // do the operation here
  this->someVariable = o.someVariable; //or something similar
}



since only one parameter is passed, the operator on the left hand side of the = sign, is the this pointer, as it would call/invoke this method.
If this method is written outside the anObject class, then it accepts 2 parameters as this has not class basis.

This post has been edited by William_Wilson: 16 April 2006 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 nobugsme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 05

Re: this pointer

Posted 18 April 2006 - 03:29 AM

There is another use for the 'this' pointer which I have come across recently but not yet found a use for is to check for validty (note I am writing this on the fly):
class Bob
{
	public:
  int HelloWorld;
	private:
  Bob(void)	{ HelloWorld = 100;}
  ~Bob(void) { HelloWorld = 100;}
  int TestMe(void)
  {
 	 // Test if the 'this' pointer is NULL 
 	 if(this != 0)
 	 {
    return HelloWorld;
 	 }
 	 else
 	 {
    return -1;
 	 }
  }
};

void main(void)
{
	Bob *Test = 0;
	Test->TestMe();	
	// Program fails the NULL/0 check and does not attempt to access HelloWorld 
	
	Bob *Test2;
	Test2->TestMe();
	// Prgram crashes because the pointer is pointing to random memory and attempting to access HelloWorld which doesn't exist
}

This post has been edited by nobugsme: 18 April 2006 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#9 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: this pointer

Posted 18 April 2006 - 08:19 PM

Although that is a possible use, taking advantage of C++ boolean abilities is a better option, as in practise this kind of catch would be caught before ever calling the testMe method with:
if(!test) { test->testMe(); } //or something similar

thought i would also point out that the testMe() function is improper, as the else statement is unessesary should be more like:
if(this != 0)
  {
   return HelloWorld;
  }
return -1;


just a better style to get used to :)
Was This Post Helpful? 0
  • +
  • -

#10 nobugsme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 05

Re: this pointer

Posted 19 April 2006 - 05:18 AM

True, it was just something I came across as I was debugging a crash bug. Like I said, I haven't come across a decent real world use for it yet ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1