can someone explain the THIS variable

This is a discussion on can someone explain the THIS variable within the C++ Programming forums, part of the General Programming Boards category; I heard there is something called "this" which refers to the class which you are in and such.... i'm trying ...

  1. #1
    Unregistered
    Guest

    can someone explain the THIS variable

    I heard there is something called "this" which refers to the class which you are in and such.... i'm trying to make a linked list of a class and in the constructor, take in a "head" varaible to keep track of the head of the list, and add the current class object to the head of the list. can someone show me a lil code for this?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    this is a pointer every class has by default; you do not need to declare it anywhere, it is hidden. Within a member function, it points to the object currently being used. If you have a member variable called data for example, anytime you access it within the function, you are actually doing this->data. You can either explicitly do this, or just forget about it since it is done for you without your knowing, it is implied. If you read up on implementing assignment operators for your classes you design, you will find it is necessary to use the this pointer explicitly.
    Last edited by hk_mp5kpdw; 07-09-2002 at 02:02 PM.
    "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
    Jul 2002
    Posts
    273
    right. you'ld have to do nasty things to get a pointer to yourself if "this" wasn't there.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If you want to use a linked lists of class objects then just use the Standard Template Library (STL). The list template object can be used to build linked lists of any data type. Here is an example using a linked list of integers.
    Code:
    #include <list>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        // Create a linked list object and an associated iterator object.
    
        list<int> lstIntList;
        list<int>::iterator itIntList;
    
        // Add elements to the list at front and back and then sort it.
    
        lstIntList.push_back( 5 );    // List is 5
        lstIntList.push_back( 10 );   // List is 5, 10
        lstIntList.push_front( 12 );  // List is 12, 5, 10
        lstIntList.push_back( 7 );    // List is 12, 5, 10, 7
        lstIntList.push_front( 16 );  // List is 16, 12, 5, 10, 7
        lstIntList.sort();            // List is 5, 7, 10, 12, 16
    
        // Print out the sorted linked list.
    
        for( itIntList = lstIntList.begin(); itIntList != lstIntList.end(); ++itIntList )
            cout << *itIntList << endl;
    
        return 0;
    }
    Last edited by hk_mp5kpdw; 07-09-2002 at 02:04 PM.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 01:30 PM
  2. help with fgets and assigning line to variable
    By pabl_o in forum C Programming
    Replies: 5
    Last Post: 04-11-2005, 10:20 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  5. Replies: 8
    Last Post: 04-22-2002, 11:02 PM

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