PDA

View Full Version : 'events' overloading in OBJ-C




Palad1
Oct 10, 2005, 07:17 AM
Hello,

I'd like to know if events can be overloaded in a subclass using OBJ-C.

Currently, I am testing out some stream related code, and needed a way to get the offset in the stream, I decided to create a custom stream class which keeps track of every read.

// Header
@interface MyInputStream : NSInputStream{
unsigned int offset;
}
- (int) read:(uint8_t) * buffer maxLength:len;
@end

// Implementation
@implementation MyInputStream
- (int) read:(uint8_t) * buffer maxLength:len
{
int read;
NSLog(@"Here I am, JH);

read=[base read:buffer maxLength:len];
if(read>0)
{
offset+=read;
}
return read;
}
@end

// calling code
MyInputStream* stream=[[MyInputStream alloc]initWithFile:filePath];
[stream read:&buffer maxLength:len]; // No log at all, the overriden message does not get fired...

I think I must be overlooking something pretty fundamental here... Could anyone enlighten me?

Thanks a lot!



robbieduncan
Oct 10, 2005, 07:23 AM
Hello,

I'd like to know if events can be overloaded in a subclass using OBJ-C.

Currently, I am testing out some stream related code, and needed a way to get the offset in the stream, I decided to create a custom stream class which keeps track of every read.

I think I must be overlooking something pretty fundamental here... Could anyone enlighten me?

Thanks a lot!

What do you mean by 'event'? Do you mean instance method? If so, yes you can redefine a message in a subclass. You cannot overload a method in terms of say C++ operator overloading.

Are NSLogs in other parts of your code working? I have seen XCode get in a state before where it does not show any NSLog output. If this is the case check the system Console output instead.

Does your code compile without errors or warnings? What is base?

Palad1
Oct 10, 2005, 08:12 AM
Hello, thanks for the reply,
I'll reply inline if you don't mind.

What do you mean by 'event'? Do you mean instance method? If so, yes you can redefine a message in a subclass.

Exactly, I'm not quite clear on the Obj-c terminology yet...


Are NSLogs in other parts of your code working? I have seen XCode get in a state before where it does not show any NSLog output. If this is the case check the system Console output instead.

I get other NSLog outputs in my log window.


Does your code compile without errors or warnings?
No warnings, No errors :)

What is base?
Well, I assumed that base was the keyword that told the message handler I wanted to use the Base implementation, the superclass...

I am coming from Java / C#, here, I assumed 'base' was the equivalent of Java's 'super' keyword or C#'s base keyword.

robbieduncan
Oct 10, 2005, 08:22 AM
I am coming from Java / C#, here, I assumed 'base' was the equivalent of Java's 'super' keyword or C#'s base keyword.

Nope. It's super in ObjC too. I've no idea what base is, but it must be defined otherwise you'd get errors.

robbieduncan
Oct 10, 2005, 08:25 AM
Have you read the subclassing notes here (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSInputStream.html)?

Palad1
Oct 10, 2005, 08:39 AM
Thanks, I missed the part about implementing my own initializers.

So, if I understand correctly, messages can be overriden, but cannot be 'virtual', hence the need to define a correct initializer...

I'll try that tonight, thanks a lot!

Edit: hmm... nops, I'm wrong. But I'm 100% positive base has been defined, I'll clean things up and post the code if it still does not work after I implement all the methods using 'super'.