On Jan 23, 2009, at 3:01 PM, Horst Jäger wrote:

suppose I have a class with a private member as listed at the end of this email.

Then I can access it by calling its name without an accessor function (which would de facto make it public).

But how do I access a private member of another object of the same class?

@interface PrivateClass : NSObject {

        @private int mineAlone;

}

-(int)yoursToo;
-(int)yoursTooButWithAnother: (PrivateClass *)another;

@end

@implementation PrivateClass

-(int)yoursToo{

        // access via name works

        return mineAlone % 3;

        // I cant't use
        // return [self mineAlone]
        // using a getter function
        // because that would make
        // mineAlone accessible for
        // everyone since a method
        // can't be declared private

There's no real way to enforce privateness, either in Objective-C or C+ +. Generally, you have to trust the code which uses your class to some extent. The typical approach is to merely make sure that the other code gets a warning from the compiler if they do something they shouldn't.

In Objective-C, this is accomplished by not declaring the method in the header. In the implementation, you can define it above where you use it. Or, you can use Objective-C 2.0 class extensions to declare it. Or, prior to Objective-C 2.0, you can define a category in your .m file to declare it.

}

-(int)yoursTooButWithAnother: (PrivateClass *)another;
        // what am I to do?
        // return another.mineAlone % 3;
        // won't work and

But another->mineAlone will.

        // return [another mineAlone] % 3;
        // would de facto make it public

See above.

}

@end

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to