• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessing private members of another object of the same class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing private members of another object of the same class


  • Subject: Re: Accessing private members of another object of the same class
  • From: Ken Thomases <email@hidden>
  • Date: Fri, 23 Jan 2009 16:25:25 -0600

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 (email@hidden)

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:
This email sent to email@hidden


References: 
 >Accessing private members of another object of the same class (From: Horst Jäger <email@hidden>)

  • Prev by Date: Re: Accessing private members of another object of the same class
  • Next by Date: Re: Core Animation
  • Previous by thread: Re: Accessing private members of another object of the same class
  • Next by thread: Re: Accessing private members of another object of the same class
  • Index(es):
    • Date
    • Thread