Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Palad1

macrumors 6502a
Original poster
Feb 24, 2004
647
0
London, UK
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.
Code:
// 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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Palad1 said:
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

macrumors 6502a
Original poster
Feb 24, 2004
647
0
London, UK
Hello, thanks for the reply,
I'll reply inline if you don't mind.

robbieduncan said:
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Palad1 said:
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.
 

Palad1

macrumors 6502a
Original poster
Feb 24, 2004
647
0
London, UK
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'.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.