[cfe-dev] Objc ivar visibility question

Jean-Daniel Dupas devlists at shadowlab.org
Tue May 4 07:02:00 PDT 2010


Hello,

I have a question about ivar visibility in Objective-C.
If you try to compile the following sample code with GCC, it refuses to compile the method -bar because _parent is a private field of Foo and you try to access it from a Bar variable.
Actually clang behaves the same.

private.m:26:27: error: instance variable '_parent' is private
 while (parent && parent->_parent)
                          ^
private.m:27:21: error: instance variable '_parent' is private
   parent = parent->_parent;


Is there a reason why GCC and clang prevent access to a Foo private ivar from a Foo method implementation ?
I know that I can simply workaround this issues with a couple of casts, but by doing this I have to feeling I'm fighting against the compiler.
Shouldn't clang be smarter and allow that ? 

================================================
@class Bar;
@interface Foo {
@private
  Bar *_parent;
}
- (Foo *)foo;
- (Bar *)bar;
@end

@interface Bar : Foo {}
@end

@implementation Foo 

- (Foo *)foo {
 Foo *parent = _parent;
 while (parent && parent->_parent)
   parent = parent->_parent;
 return parent;
}

- (Bar *)bar {
 Bar *parent = _parent;
 while (parent && parent->_parent)
   parent = parent->_parent;
 return parent;
}

@end
==================================================


-- Jean-Daniel




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100504/f7a7f201/attachment.html>


More information about the cfe-dev mailing list