Results 1 to 2 of 2

Thread: Classes

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2000
    Location
    Kingswood, UK
    Posts
    6

    Classes

    Does anyone know how to get an object to know itself?

    I mean, like in VB you can use 'Me'

    Eg.

    class Room
    {
    Room* Exit[4];
    void Make_Exit(Room* New_Room, int Direction)
    {
    int Noitcerid = (Direction + 2) % 4; //Opposite direction

    Exit[Direction] = New_Room;

    New_Room->Exit[Noitcerid] = Me;
    }
    }

    Room Lounge, Kitchen;

    void Init(void)
    {
    Lounge.Make_Exit(Kitchen,0);
    }


    And this would make an exit from Lounge to Kitchen and from Kitchen to Lounge, if 'Me' worked!

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Have a look at the this pointer:
    Code:
    this
    C++ Specific —>
    
    The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
    
    When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call
    
    myDate.setMonth( 3 );
    can be interpreted this way:
    
    setMonth( &myDate, 3 );
    The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.
    
    The expression (*this) is commonly used to return the current object from a member function.
    
    Note   Modifying the this pointer is illegal in the latest version of C++.
    
    END C++ Specific
    
    Example
    
    // Example of the this pointer
    void Date::setMonth( int mn )
    {
       month = mn;            // These three statements
       this->month = mn;      //     are equivalent
       (*this).month = mn;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width