discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Life of instance variables?


From: Kim Shrier
Subject: Re: Life of instance variables?
Date: Sun, 25 May 2003 02:33:02 -0500
User-agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.1) Gecko/20021214

It would be nice ro see the source of the init method for Node and MyNode.
I am going to assume your code is something like this:

@implementation Node

-init
{
if ([super init] == nil) {
return nil;
}

state = 14;

// other initialization

return self;
}

@end

The init method for MyNode can be inherited from Node or if you have
addition initialization, I assume you have done something like:

@implementation MyNode

-init
{
if ([super init] == nil) {
return nil;
}

// other MyNode initialization

return self;
}

@end


If you have done that, your instance variable will be properly initialized
and its value should be valid as long as the instance is not freed. Any
value you set with setNodeState should stay around until you change it with
setNodeState.

If you are seeing a value of 14 for state and you are sure that you are not
setting it to 14 after you have set it to 1, then I would make sure that you
are passing the instance of MyNode whose state was set to 1 into your
Channel
instance.

Kim

Albert Chun-Chieh Huang wrote:

>Dear all,
>    My question is about instance variable. Let me explain in details:
>Node and MyNode is two classes, their relationship is 
>MyNode : Node : NSObject, where MyNode is a subclass of Node.
>In my code, there is only instances of MyNode, for Node is an abstract
>class. 
>
>@interface MyNode : Node
>{
>   some instance variables
>}
>- init;
>@end
>
>@interface Node : NSObject
>{
>   int state;   // Initial value as 14
>}
>- init;
>- (void)receivePacket: (id)aPacket;
>- (void)setNodeState: (int)aState;
>@end
>
>@implementation Node
>- (void)setNodeState: (int)aState
>{
>   state = aState;
>}
>@end
>
>I create the instance of MyNode in another class, say Simulator, by:
>aNode = [[MyNode alloc] init];
>[aNode setNodeState: 1];
>
>In the class Channel:
>[aNode receivePacket: aPacket];  // aNode is an instance of MyNode
>
>So I assume that when I call [aNode receivePacket: aPacket], state is
>not possible to be 14 because the initial value won't be set again!
>I'm sure that I didn't set it to 14 in other places. And now, I add
>some NSLog in -[Node receivePacket:], I found that whenever I call
>[aNode receivePacket: aPacket], "state" value is 14 not 1. Why did
>this append? 
>
>I don't know why it happens. I studied this in "Object-Oriented
>Programming and the Objective-C Language" manual from NeXT Developer's
>Library, and I couldn't find the reason. The instance variable "state"
>should be "inherited" from Node. And when I call 
>[aNode receivePacket: aPacket], the run-time system should find the
>superclass of MyNode because MyNode doesn't have implementation of
>receivePacket: method. Sorry, I'm really confused. Could anyone be 
>kind of telling me why? Thanks in advance. 
>
>Best Regards,
>Albert
>
>

-- 
 Kim Shrier - principal, Shrier and Deihl - mailto:kim@tinker.com
Remote Unix Network Admin, Security, Internet Software Development
  Tinker Internet Services - Superior FreeBSD-based Web Hosting
                     http://www.tinker.com/






reply via email to

[Prev in Thread] Current Thread [Next in Thread]