Re: OOP Clarification
Re: OOP Clarification
- Subject: Re: OOP Clarification
- From: "Clark S. Cox III" <email@hidden>
- Date: Fri, 4 Jan 2002 18:20:39 -0500
On Friday, January 4, 2002, at 05:41 , Rick wrote:
On Friday, January 4, 2002, at 10:25 AM, Marco Scheurer wrote:
On Friday, January 4, 2002, at 04:40 pm, Smith, Bradley wrote:
When you say you can't overload static methods I'm not sure what you
mean.
I meant overriding. Can you redefine a static method in a subclass and
invoke the base class implementation?
C++ Static methods cannot be redefined in that sense as they don't use
the virtual keyword and polymorphism thus doesn't apply. It is perfectly
legal though to do the following:
class A
{
static void foo (void);
};
void A::foo (void) { }
class B : public A
{
static void foo (void);
};
void B::foo (void)
{
A::foo();
}
Above, you have the same static method (name and args) in both the parent
and derived class. However, the one in B doesn't override the one in A.
Static methods don't have a "this" pointer, so there's nothing to apply
polymorphism to.
Same applies to Obj-C...you don't have "self" and "super" inside those
class methods, so you can't override or access parent class methods. But,
just as shown above, you can have the same class methods in multiple
Obj-C classes.
No, class methods in Obj-C *do* have a self object, it's the class itself:
[localhost:~] clarkcox% cat test.m
#import <Foundation/Foundation.h>
@interface Foo : NSObject {}
+foo;
@end
@interface Bar : Foo {}
@end
@implementation Foo
+foo
{ return [ [ [ self alloc ] init ] autorelease ];
}
@end
@implementation Bar
@end
int main()
{
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ]
init ];
NSLog( @"%@", [ [ Bar foo ] class ] );
[ pool release ];
}
[localhost:~] clarkcox% cc test.m -framework Foundation
[localhost:~] clarkcox% ./a.out
2002-01-04 18:19:27.841 a.out[17702] Bar
[localhost:~] clarkcox%
--
Clark S. Cox, III
email@hidden
http://www.whereismyhead.com/clark/