I am new to programming.I have seen this code.returning a derived class object to the base class.
So that the base class can then point to the derived class methods.
Here a static function in class B is returning its object to the base
class.
base-derivedclass.m
#import <Foundation/Foundation.h>
#import "B.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[B p];
[pool drain];
return 0;
}
A.h
#import <Foundation/Foundation.h>
@interface A : NSObject {
}
@end
A.m
#import "A.h"
@implementation A
@end
B.h
#import <Foundation/Foundation.h>
#import "A.h"
@interface B : A {
}
+(A*)p;
-(void)display;
@end
B.m
#import "B.h"
@implementation B
+(A*)p
{
NSLog(@"returning derived class object to the base class!!");
return [B new];
}
-(void)display
{
NSLog(@"Hello");
}
@end
pis a class method. In Obj-C you denote class method by using+in method declaration and-to denote instance method. You can call the class method by using this:Or you can change
pto instance method by this:You can check Objective-C: A Primer to get started with these.