Reputation: 4997
I've got two classes: Alpha and Beta.
Beta inherits from Alpha.
How do I write a method in Alpha such that if I call it with an object of class Beta, it will return a new Beta object?
For example:
- (Alpha *)makeAnother {
return [[[self class] alloc] init] autorelease];
}
I thought this would work, but it doesn't.
Upvotes: 0
Views: 462
Reputation: 162722
- (Alpha *)makeAnother {
return [[[self class] alloc] init] autorelease];
}
That code works fine. Without you defining what "doesn't work" means to you, hard to say more.
However, you should define the method as returning (id)
if you are ever going to expect a pattern like this to work without casting:
Beta* b = [someObjectIKnowIsBeta makeAnother];
Objective-C doesn't support co/contra variance and, thus, any method that is expected to return instances of different classes is declared as returning (id)
to avoid a cast. This is common across the frameworks. See -dataCell
, +array
, +alloc
, etc...
Upvotes: 0
Reputation: 94783
The easiest way would be to write your alpha function:
- (Alpha *)makeAnother {
return [[[Alpha alloc] init] autorelease];
}
and then overwrite that function in your beta class:
- (Alpha *)makeAnother {
return (Alpha*)[[[Beta alloc] init] autorelease];
}
Upvotes: 0
Reputation: 3461
Not sure what you're trying to do here, when you use polymorphism the ultimate goal is to create the proper subclass, Beta, and it inherits all of the methods of superclass, Alpha. If you want to return an object of type Beta you should be declaring it in the Beta class.
If the issue is that you have circular dependencies, then you can semi-declare a needed but not yet declared class inside of another class by using this little trick:
@Class Beta;
It would help if you tried to describe exactly what you're trying to do with your code.
Upvotes: 0
Reputation: 20236
Change the return type from Alpha*
to id
in this case. The rest of it is right.
Upvotes: 2