Amazon.com Widgets Overriding init and alloc
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 01, 2014, 02:48:28 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 10
| | |-+ Overriding init and alloc
Pages: [1] Go Down
Print
Author Topic: Overriding init and alloc (Read 2679 times)
Hesadanza
Newbie
*
Posts: 28






on: February 11, 2012, 04:59:34 PM

I'm confused about the difference between overriding alloc and init.  Overriding init seems to be accepted, while overriding alloc isn't.  There is a warning that overriding alloc is not considered good programming practice.

Yet in both examples, for init and alloc, they are overridden the same way in the book, albeit one is an instance method, and the other is a class method.  A new method is written, like initSet:over: or allocF, and init and alloc are called within those methods, respectively.

Couldn't you directly override alloc too (i.e. with an alloc class method), and just make sure you call super alloc within your method similar as was done in allocF?  Is there a danger in this?
Last Edit: February 11, 2012, 05:02:56 PM by Hesadanza Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 22, 2012, 10:48:36 AM

Yes, you can override alloc.  But you really should have a good reason to do so.   Implementing a singleton in your class could be one reason, but if you want to allocate space yourself, you'd need to get involved in the memory allocation details for the object, which is why you generally don't want to override dealloc.   Calling [super dealloc] would only allocate space for the instance variables in the parent class, and not the instance variables added in your class.  So it can get a little tricky.

Cheers,

Steve
Logged
starfall
Newbie
*
Posts: 3


Email




Reply #2 on: March 28, 2012, 11:10:32 AM

Yes, you can override alloc.  But you really should have a good reason to do so.   Implementing a singleton in your class could be one reason, but if you want to allocate space yourself, you'd need to get involved in the memory allocation details for the object, which is why you generally don't want to override dealloc.   Calling [super dealloc] would only allocate space for the instance variables in the parent class, and not the instance variables added in your class.  So it can get a little tricky.

Cheers,

Steve

I think you mean "alloc" instead of "dealloc" ?
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.