Objective-C basics: subclassing and member variable accessability
Posted
by Krumelur
on Stack Overflow
See other posts from Stack Overflow
or by Krumelur
Published on 2010-05-19T14:19:56Z
Indexed on
2010/05/19
14:20 UTC
Read the original article
Hit count: 330
objective-c
Hi,
Code below is pseudo code. Imagine a class "Fruit" which has a factory method to create a fruit.
interface Fruit
{
}
+(Fruit*) createFruit:
{
return [[Fruit alloc] init autorelease];
}
Now I want to subclass the Fruit to get an Apple:
interface Apple : Fruit
{
int iSeeds;
}
+(Apple*) createAppleWithColor: (int) iSeeds
{
Apple* oApple = [Apple createFruit:];
oApple.iSeeds = iSeeds;
return oApple;
}
Questions:
- How can I make "iSeeds" private so it cannot be changed from outside? If I add a "private" keyword it does not build anymore.
- Still I want to set iSeeds from inside my Apple's factory method.
- I want users allow to READ the content of iSeeds. So I suppose I should have a getter but I can't get it to work. I always get some error about "LValue assignment".
- The Fruit's createFruit is making use of autorelease. Does the Apple have to reatin/release anything?
René
© Stack Overflow or respective owner