 |
 |
|
 |
06-28-2009, 12:20 PM
|
#1 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
accessing containing class variable from subclass
Hi everyone!
I know this is gonna sound "n00b", but i recently moved to obj-c, so i'm not very familiar with it yet.
Here's my question :
I have a class A with a member variable which is a class B.
So the class B does NOT inherit from class A, but it is a subclass (is this the right term?) of the class A.
Now, in one of the methods of class B, i need to access a member variable of class A which is public, and i can't figure out how to do it!!
Please do not give me an answer such as "why don't you change the architecture" because this is just a simple example, and my real architecture is way more complicated, but the problem summarize as above.
I tried to use "superclass" and "super" but i think this is only for inherited classes, right?
Can someone help me?
|
|
|
06-28-2009, 01:08 PM
|
#2 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
Quote:
Originally Posted by neduhamel
So the class B does NOT inherit from class A, but it is a subclass (is this the right term?) of the class A.
|
If class B does not inherit from class A, then "subclass" is probably not the right word. Subclass would imply, to me, that B inherits from A. Let's say they have a parent-child relationship instead.
If class B only needs to know its parent in a certain method, you could pass the parent into that method. Otherwise, if B needs to contact its parent a lot, you could give class B an instance variable called "parent" , and write an initWithParent: method that sets the parent variable. If the parent changes a lot, you should probably make b.parent a property instead.
__________________
|
|
|
06-29-2009, 01:55 AM
|
#3 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
Hi Smasher !
Thanks a lot for your reply.
the "initWithParent" sound like a good idea, but my problem is that i don't know technicaly how to define the parent class as a member of the child class.
i would guess something like this :
-(void) initWithParent
{
my_parent = super;
}
is this the right way? And then i can access is using:
x = my_parent.member_data;
|
|
|
06-29-2009, 02:18 AM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
Quote:
Originally Posted by neduhamel
Hi Smasher !
Thanks a lot for your reply.
the "initWithParent" sound like a good idea, but my problem is that i don't know technically how to define the parent class as a member of the child class.
|
How did you define the child class as a member of the parent class? You need the same thing - an instance variable and possibly a property.
This is how you'd define the initWithParent method. It will look funny if you've never written an init method before.
Code:
-(void) initWithParent: (id) parent
{
self=[super init];
if (self){
my_parent=parent;
}
return self;
}
Note that I used "super" here because I need to call the init method of the class that B inherits from (B's superclass; probably NSObject?)
__________________
|
|
|
06-29-2009, 03:05 AM
|
#5 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
i'm sorry, i feel like if i do as you say, it's gonna create a endless loop as the child will have a member which is the parent which has a child which has a parent....and so on. Am i wrong?
Here's what i understand from your advice:
Code:
//class_A.h
//-----------
@interface class_A
{
@public
class_B my_class_B;
}
-(void)init;
@end
Code:
//class_A.m
//-----------
@implementation class_A
-(void) init
{
[my_class_B initWithParent:self]; //is this ok?
}
@end
Code:
//class_B.h
//------------
@interface class_B
{
@private
class_A my_parent; //This what i doubt is good...
}
-(void) initWithParent: (id) parent;
@end
Code:
//class_B.m
//------------
@implementation class_B
-(void) initWithParent: (id) parent
{
self=[super init];
if (self){
my_parent=parent;
}
return self;
}
@end
|
|
|
06-29-2009, 06:31 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 61
Posts: 505
|
From the interface files for class_A and class_B, it seems that B is indeed not a subclass of A. They are just two classes that need to know about each other. So make sure each class has a #import directive for the other class's interface file. When you say that class A contains an instance of class B, that has nothing to do with subclassing or inheritance. If that is the case, then go ahead and write initWithParent as suggested by smasher. Although "initWithOwner" might be a more appropriate name if class A and class B do not have any subclass relationship. In your class B interface file, define an instance variable that is a pointer to an instance of class A, such as:
Class_A *myOwner;
In your initWithOwner method, set myOwner equal to the parameter that is passed by Class A. Since you are holding on to this pointer for some length of time, you should probable define it as a retained ivar and use the setter method to set it, as in:
self.myOwner = parameterPassed;
and then remember to release myOwner in your Class B dealloc method. Now whenever you want to access an instance variable of Class A from within a Class B method, just use:
myOwner.someAvariable
Robert Scott
Ypsilanti, Michigan
|
|
|
06-29-2009, 12:53 PM
|
#7 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
Great, it works!!
Thank you very much to both of you!
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 374 |
| 34 members and 340 guests |
| acmecorps, bandley, bensj, bobvanratingen, brianr, chouchou, CHV, cmazzi, Cooper_1988, DaveM, drakan, eieiosoftware, firearasi, gandohr, gbh, goodcode, henmue, iGeorG, Kilby, lbergelt, nikozster, omerk, pengcognito, pikabli, quky, Raazo, Rainer, salvah, shark4ever, SkipLo, supernico, Trindaz, wassupdoc, yeohchan |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,292
Threads: 39,080
Posts: 171,361
Top Poster: smasher (2,575)
|
| Welcome to our newest member, omerk |
|