title: self in Objective-C
body: <p>is <code>self</code> not completely interchangeable with <code>this</code> in C++?</p>
  
  <p>It seems to work with message passing (<code>[ self sayHi ]</code> would work within any method there).</p>
  
  <p>I don't quite understand why I can't use self to access private members of an object (in the example below, I show I can't use <code>self.width</code>)</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  // Write an Objective-C class
  @interface Rectangle : NSObject
  {
    int width ;
  }
  
  -(int)getWidth;
  -(void)setWidth:(int)w;
  -(void)sayHi;
  -(void)sayHi:(NSString*)msg ;
  @end
  
  @implementation Rectangle
  
  -(int)getWidth
  {
    // &lt;b&gt;return self.width ; // ILLEGAL, but why?&lt;/b&gt;
    // why can't I return self.width here?
    // why does it think that's a "method"?
    return width ;
  }
  -(void)setWidth:(int)w
  {
    // &lt;b&gt;self.width = w ; // ILLEGAL&lt;/b&gt;
    // again here, I CAN'T do self.width = w ;
    width = w ;
  }
  -(void)sayHi
  {
    puts("hi");
  }
  -(void)sayHi:(NSString*)msg
  {
    printf( "Hi, and %s\n", [ msg UTF8String ] ) ;
  }
  
  @end
  
  int main (int argc, const char * argv[])
  {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  
    Rectangle* r = [ Rectangle alloc ] ;
    [ r sayHi ] ;
    [ r setWidth:5 ] ;
    printf( "width is %d\n", [ r getWidth ] ) ;
  
    [pool drain];
    return 0;
  }
  </code></pre>