'events' overloading in OBJ-C

Discussion in 'Mac Programming' started by Palad1, Oct 10, 2005.

  1. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #1
    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!
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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?
     
  3. thread starter macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #3
    Hello, thanks for the reply,
    I'll reply inline if you don't mind.

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

    I get other NSLog outputs in my log window.

    No warnings, No errors :)

    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.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Nope. It's super in ObjC too. I've no idea what base is, but it must be defined otherwise you'd get errors.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  6. thread starter macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #6
    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'.
     

Share This Page