this?

This is a discussion on this? within the C++ Programming forums, part of the General Programming Boards category; I have a question about the following code: Code: Class Node { public: Node *next; int data; Node(){ next = ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    30

    this?

    I have a question about the following code:

    Code:
    Class Node
    {
    public:
    	Node *next;
    	int data;
    	Node(){ next = 0, data = 0;}  //...constructor...
    
    	bool Load( char *filename )
    	{
     		//...open file for reading...
     
    		Node *t = this;
     		while(t->next !=NULL)
     		{
       			t = t->next;
     		} 
    		while(!level.eof())
    		{
     			Node *born = new Node;
     			if(born == NULL)
     			//...couldn't allocate, close file and return false...
    
    			//...read in data...
     			t->next = born;
    			t= t->next;
    		}
    	//...close file and return true...
    	}
    
    	void Kill()
    	{
      		Node *t = this;
      		if( t->next == NULL)
      			return;
    
     		 t = t->next;
    
      		while(t->next != NULL)
      		{
        			loser = t; 
        			t = t->next;
        			delete loser;     
      		}
     	delete t;
    	}
    };
    
    int main()
    {
     	Node head;
    	head.Load("myfile.dat");
    	head.Kill();
    	head.Load("another.dat"); 
    	head.Kill();
     	return 0;
    }
    What does the 'Node *t=this' do? And can a list created as in the main function be used by other functions (ie you load the list in the main function and 'kill' it in another function or something)?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    this is a pointer that every class has by default that refers to itself. So, that statement simply creates a pointer t that points to that particular instantiation of the class.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    30

    And what does that mean?

    What would be the particular instantiation of the class in this case? From what the code looks like I thought it would be like jumping to the beginning of the list, is that so? If not how would one do it if the code looks like this? I mean can it be done differently than simply creating a head? But then the list created in this example wouldnŽt be able to be used if you base your traversing code from a head, would it?

    And what about my other question?
    Last edited by ninja; 05-20-2002 at 12:09 PM.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    30

    IŽve really tried to understand this...

    ...but it seems none of the wensites IŽve visited can explain it understandably, and I canŽt find anything about searching previous posts. Could someone here please assist me in learning about this? And perhaps tell exactly what the code I refered to does (listwise that is)?
    If the code works as I thought, is it the best way to do what it does, or is there a better way to delete a list and then load information into it again?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    This --->

    Originally posted by hk_mp5kpdw
    this is a pointer that every class has by default that refers to itself. So, that statement simply creates a pointer t that points to that particular instantiation of the class.
    Added to that This is the pointer that point to the thing that you are dealing with right now... YOU DON"T CREATE it, it is created By C++ ...

    Use it,..... it is Very friendly
    C++
    The best

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    30

    OK, thanks!

    IŽll try...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21