Class variable for Objective-C and C++

It is said that there is no class variable for Objective-C. No, there is no class variable in Objective-C. However, it doesn’t mean that concept of class variable doesn’t exist for Objective-C.

I have heard that many of Korean Objective-C programmers studied the language with my Objective-C reference document. It is very poorly written and contains lots of typos, wrong sample and misleading explanation. Actually it was not written for people who want to learn Objective-C. It was written to explain differences among C++, Java and Objective-C at first, but its purpose was changed while it was being written. So, for people who don’t know history of computer languages and their general terminology would be confused. So, some may will raise question for a statement I wrote in the PDF file. I mentioned that class variable existed in Objective-C. (conceptually)
However, it seems that there are still a few people are doubtful about what I said.
So, I decided to write this post.

Anyway, let’s address class variable of Objective C here.
For C++, class variable can be declared and used like this.

#include <iostream>
using namespace std;

class CTest
{
    // declaration of a class variable
    static int numInstance;

public:
    CTest() { numInstance++; };

    void DisplayNumInstance( void )
	{ cout << "numInstance : " << numInstance << endl;};

};

int CTest::numInstance = 0;

int main (int argc, char * const argv[]) {

    CTest *one = new CTest;
    one->DisplayNumInstance(); //Print 1

    CTest *two = new CTest;
    two->DisplayNumInstance(); // Print 2

    delete one;
    delete two;

    return 0;
}

UPDATED : The source codes above for C++ are updated. A while ago, WordPress.com updated the tag syntax for embedded source codes. At that moment, I found out that my formatted source codes were not displayed as they were. So, I updated the tags. However, it should have been done in HTML editing mode, not Visual mode. While I was editing it, probably I pressed the “Visual” tag tab. After that point, it seems to me that every thing was messed up. Some part of codes were gone, and so on. I didn’t noticed that the source codes were messed up.
So, I updated it now.

So, strictly speaking, C++ doesn’t support class variable also. A static variable declared in a class is used as a class variable, and C++ reference just says that it supports class variable.
Because the C++ doesn’t support class method like Objective-C does, a class variable should be defined as shown above.
(Chris:P pointed said that class method in C++ works the way that of Obj-C does. So, I kind of removed this line. Thank you, Chris:P! )
( Here. class method doesn’t mean a method declared in a class. The class in “class method” here means the “class” which is counter part of “instance”.
Theoretical OOP terminology is confused due to choice of terminology used for C++. )

Now, let’s take a look at Objective-C case.

#import <Foundation/Foundation.h>

static int gNumOfInstance;

@interface sampleEntity : NSObject {

	int mNum;
}

+ (void) initialize;

- (id) init;
- (void) displayNumOfInstance;

@end

[Source 1] sampleEntity.h

#import <Foundation/Foundation.h>
#import "sampleEntity.h"

@implementation sampleEntity

+ (void) initialize
{
	gNumOfInstance = 0;
}

- (id) init
{
	self = [super init];
	if (self != nil) {
		gNumOfInstance++;
	}
	return self;
}

- (void) displayNumOfInstance
{
	NSLog(@"Number of instances of this class : %d", gNumOfInstance );
}

@end

[Source 2] sampleEntity.m

#import <sampleEntity.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	sampleEntity *myEntityOne = [[sampleEntity alloc] init];
	[myEntityOne displayNumOfInstance]; // Print 1

	sampleEntity *myEntityTwo = [[sampleEntity alloc] init];

	[myEntityOne displayNumOfInstance]; // Print 2

	[myEntityOne release];
	[myEntityTwo release];

    [pool drain];
    return 0;
}

UPDATED : The source codes above for C++ is updated. Some characters were shown as HTML special characters. So, I corrected them.

gNumOfInstance is declared in sampleEntity.h as a global variable. So, its scope is confined in where sampleEntity.h is included. However, it is not enough. instance variable is used in a class for it only. So, a keyword “static” is placed in front of its declaration for being sure. (Because it is accessible in source files which include classes’ header files, you should be careful not to use same variable names. If you don’t want this, and really want to make it to be available only in its class, declare it in implementation files, i.e. *.m, of classes. )

In the case of Objective-C, it supports class methods. Again, the “class” here means the “class” which is a template of its “instance”. So, you can manipulate the class variable in its class method like +(void)initialize.

So, by looking at this, can you say Objective-C doesn’t support class variable? If you say that Objective-C doesn’t support, can you also say C++ doesn’t support it? For C++ case, its compiler designers used “static” variable as a class variable semantically. For Objective-C, you can’t declare a static variable in a class. ( It is meaningless even if Obj-C compiler allows it, if it is not interpreted as something else. ) Obj-C is a simple super set of C. So, the Obj-C compiler designer didn’t want to change many things to C compiler. So, eventually, it didn’t have any compiler support for class variable. Instead, you can use a global variable declared in a class file, no matter that it is a header file or an implementation file, as a class variable.

FYI, I put a screentshot here which shows what happens if you declare a static variable in a class.
picture-1

19 responses to this post.

  1. […] Class variable for Objective-C and C++ – JongAm’s blog […]

    Reply

  2. […] Class variable for Objective-C and C++ – JongAm’s blog […]

    Reply

  3. […] Class variables explained comparing Objective-C and C++ approaches […]

    Reply

Leave a comment