Re: Making init return a subclass of the Class being init-ed & circular imports
Re: Making init return a subclass of the Class being init-ed & circular imports
- Subject: Re: Making init return a subclass of the Class being init-ed & circular imports
- From: Thomas Harrington <email@hidden>
- Date: Mon, 28 Jul 2003 21:12:35 -0600
On Monday, July 28, 2003, at 06:55 PM, Diggory Laycock wrote:
There are several different standard NMEA sentence-types (containing
different kinds of data. - e.g. some describe the Number of Satellites
acquired, others describe Geographic Position etc.. )
My plan to is to have subclasses of this standard sentence object. I
can then add methods to these subclasses that are relevant to the
content of the sentence-type.
So far so good - but what I really want to do is to get the Parent
Generic Sentence Class (MFNMEASentence) to be able to return a
specific subclass of itself depending on the sentence-type it is
initialised with. (falling back to returning the generic sentence
object if there is no appropriate subclass )
I'm afraid I'm not entirely sure of the best way of doing this - Here
is my best shot:
so far I have something a bit like the code below in MFNMEASentence:
I have a couple of questions -
1 - Is this a good idea? Should I be doing it like this?
I would have made it a more proper "factory" method, i.e. something
like:
+ (id)sentenceFromString:(NSString *)initString;
But then my OO theory is a bit rusty, so I'm not sure that's better or
worse than what you came up with. It's a valid approach, though
depending on other details of the situation you might want to
investigate class clusters.
2 - The code doesn't actually compile - it's got some kind of circular
import going on
- MFNMEASentence.h (generic superclass) has to import
MFNMEARMCSentence.h (the subclass) because it uses it when it init's
the subclass.
- but MFNMEARMCSentence.h has to import MFNMEASentence because it is
a subclass of it.
so I get the following error when I try to compile:
MFNMEARMCSentence.h:37: cannot find interface declaration for
`MFNMEASentence', superclass of `MFNMEARMCSentence'
You can probably break this circle through the use of "@class". You
don't need to #import unless you need to know the full class interface.
If it's enough to just know that a class exists, and the details can
be worried about later, use @class.
--
Tom Harrington
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.