|
-
Jun 8th, 2001, 08:28 AM
#1
Thread Starter
New Member
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!
-
Jun 8th, 2001, 11:18 AM
#2
Frenzied Member
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|