Link to home
Start Free TrialLog in
Avatar of Schuchert
Schuchert

asked on

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.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

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
Avatar of Schuchert
Schuchert

ASKER

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.
>>>> 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),
   
>>>> 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.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That more or less answers it for me. The class method does not need an actual instance to call the method on.