• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Creating a class cluster
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating a class cluster


  • Subject: Re: Creating a class cluster
  • From: Greg Titus <email@hidden>
  • Date: Sun, 28 Jul 2002 18:06:39 -0700

On Sunday, July 28, 2002, at 05:54 PM, Shawn Erickson wrote:

On Sunday, July 28, 2002, at 05:04 PM, David Newberry wrote:

If I don't have this code which forwards the messages to the subclasses, I get errors when sending an init... message to my superclass (eh '[[MyClass alloc] initWithPath@"/"]'. Is this appropriate? It seems alternatively that people have said "yes" and "no"... but chances are I just misunderstood something.

You are calling alloc (a class method) on your super class. This is creating an instance of your super class which you then send an initXxxx to. If you want to return something other then you super class then you need to implement the initXxxx methods to do so. This isn't some much forwarding as implementing things as they need to be to get done what you want to get done.

Also, from what Ondra said, do I take it that (if this code is proper), those methods should each start with "[self autorelease];"?

Yes, you need to insure the class instance that was allocated by the alloc call against your super class is freed.

An alternative to this is to have your super class be a singleton by implementing +allocWithZone:.

+ alloc
{
static MySuperClass *sharedInstance = nil;

if (!sharedInstance)
sharedInstance = [super alloc];
return sharedInstance;
}

That way, you aren't creating and deleting an instance of your superclass every time you alloc and init your subclass. Instead, alloc always returns the same object, and then init exchanges it for one of your subclass objects.

Hope this helps,
-Greg
_______________________________________________
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.

  • Follow-Ups:
    • Re: Creating a class cluster
      • From: Ondra Cada <email@hidden>
References: 
 >Re: Creating a class cluster (From: Shawn Erickson <email@hidden>)

  • Prev by Date: Help with NSDictionary!
  • Next by Date: Re: More memory allocation questions
  • Previous by thread: Re: Creating a class cluster
  • Next by thread: Re: Creating a class cluster
  • Index(es):
    • Date
    • Thread