Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2074
  • Last Modified:

Class method explained in Objective-C

Can someone explain what a class method is or better yet what an example would be? I have looked through my Cocoa book and documentation online but I am not getting it. I understand instance methods but I can't figure out what a class method would be used for.
0
Schuchert
Asked:
Schuchert
  • 2
  • 2
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Schuchert,

A class method is a method (function) that operates exclusively on an object of the specific class.

In conventional C coding, you often pass the address of a struct to a function.  Object oriented languages give us the term "class".  A class is a superset of a typedef in that the class declares all of the data members in the class (just like a struct does), but also declares all of the functions (methods) that are specific to the class.  Class member functions (methods) typically can not be called on objects that are not members of the class.


Kent
0
 
SchuchertAuthor Commented:
I understand that. The part I don't understand is what an example of a class method would be. Like if you had a friend class. I understand that you could have an instance method to give the friend a name and age, etc. I need an example of a class method.
0
 
itsmeandnobodyelseCommented:
>>>> I understand instance methods but I can't figure out what a class method would be used for.

The terms 'instance methods' and 'class methods' are not widely used in the C/C++ world (and I am not sure whether these terms have a unique definition).

But if a 'instance method' is a 'member function' of a class because it can only called on objects (instances) of that class, then necessarily a 'class method' is somewhat different. I would assume it is that what in C++ is a 'static member function' what is a function defined in a class and which can be called without having an instance of the class, i. e. it runs on the meta object class itself.

class X
{
     int i;
public:
     X(int j) : i(j) {}
     int memberFunc() { return i; }
     static int staticMemberFunc() { return 5; }
};

Now you can do:

   X  x(10);
   int n;

   n = x.memberFunc();  // now n = 10
   n = x.staticMemberFunc(); // now n = 5
   n = X::staticMemberFunc(); //  n is still 5

   n = staticMemberFunc();  // doesn't compile

You see a static member function is a global function which was defined in a class. If it is protected or private it only can used by other member functions (or from member functions of dericved classes if protected). These static functions can be used on an object of the class or by using the class scope (X:: in the sample) but - as the static functions have no instance can not access non-static data members (i in the sample)

int X::staticMemberFunc()
{
     return i;   // error i is undefined
}


So if using static member functions you often have static data members as well where the static functions have access to:

class X
{
     static int sumi;  // sum of i of all instances  
     int i;
public:
     X(int j) : i(j) { sumi += i; }
     ~X()  { sumi -= i; }
     int memberFunc() { return i; }
     static int staticMemberFunc() { return sumi; }
};

Here the constructor (which is a non-static member function) updates the sumi and the destructor would subtract. The static member function has access only to the static data member sumi but not to the non-static members (data or functions),
   
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
itsmeandnobodyelseCommented:
>>>> Like if you had a friend class.
No, a friend class only means that member functions of the class can access protected or private data member of the class where it is declared as friend of. Also it can call private or protected member functions of the friend class so at if they were declared as public.

friend classes were often used by third party libraries to put a big part of internal implementation to a second class which is related only by pointer to its friend. Hence they don't need to supply the header and the sources of the second class thus hiding their knowhow.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Schuchert,

The difference is subtle, but there is a difference.

--  An instance method is invoked from a specific object. To run an instance method, an object must exist in memory.
--  A class method does not require a specific instance. It can be used even if no instance of the class exists in memory

Kent
0
 
SchuchertAuthor Commented:
That more or less answers it for me. The class method does not need an actual instance to call the method on.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now