{"id":4872,"date":"2014-03-30T16:15:43","date_gmt":"2014-03-30T16:15:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/objective-c-static-class-method-definition-what-is-the-difference-between-static-and-collection-of-common-programming-errors\/"},"modified":"2014-03-30T16:15:43","modified_gmt":"2014-03-30T16:15:43","slug":"objective-c-static-class-method-definition-what-is-the-difference-between-static-and-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/objective-c-static-class-method-definition-what-is-the-difference-between-static-and-collection-of-common-programming-errors\/","title":{"rendered":"objective-c static\/class method definition &#8211; what is the difference between &ldquo;static&rdquo; and &ldquo;+&rdquo;?-Collection of common programming errors"},"content":{"rendered":"<p>Unlike in (say) C++, where static member functions are just ordinary functions in the class&#8217; namespace, Objective-C has proper class methods.<\/p>\n<p>Since classes are objects, calling a class method is really like calling an instance method on the class. The main consequences of this are:<\/p>\n<p>1) Calling a class method incurs a slight (although generally inconsequential) overhead, since method calls are resolved at runtime.<\/p>\n<p>2) Class methods have an implicit &#8216;self&#8217; argument, just like instance methods. In their case, &#8216;self&#8217; is a pointer to the class object.<\/p>\n<p>3) Class methods are inherited by subclasses.<\/p>\n<p>together, 2 and 3 mean that you can do stuff like this with a class method:<\/p>\n<pre><code>+ (id) instance\n{\n    return [[[self alloc] init] autorelease];\n}\n<\/code><\/pre>\n<p>then create a new class that inherits the method and returns a new instance of itself, rather than the superclass.<\/p>\n<p>I believe that marking an ordinary c function static will just make it unavailable to files other than the one it&#8217;s defined in. You&#8217;d generally do this if you wanted to make a helper function that is only relevant to one class and you wanted to avoid polluting the global namespace.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike in (say) C++, where static member functions are just ordinary functions in the class&#8217; namespace, Objective-C has proper class methods. Since classes are objects, calling a class method is really like calling an instance method on the class. The main consequences of this are: 1) Calling a class method incurs a slight (although generally [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4872","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4872"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4872\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}