Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of static member in objective-C and objective-C++

I have a difference when compiling objective-c source and objective-c++ source.

Here a declaration of Class1 and Class2 in test.h :

#import <Foundation/Foundation.h>

@interface Class1 {
}
@end

@interface Class2 {
}
@end

Now, this is Objective-C implementtion in test.m :

#import "test.h"

@implementation Class1
/* static member */
static int mystatic;
@end


@implementation Class2
/* static member */
static int mystatic;
@end

I compile successfully with this command :

gcc -arch armv6 -isysroot /Developer/.../iPhoneOS5.0.sdk -x objective-c -c test.m

Now I use exactly the this Objective-C++ implementation test.mm (exactly same source) :

#import "test.h"

@implementation Class1
/* static member */
static int mystatic;
@end


@implementation Class2
/* static member */
static int mystatic;
@end

And compile with this command line (difference in -x option) :

gcc -arch armv6 -isysroot /Developer/.../iPhoneOS5.0.sdk -x objective-c++ -c test.mm

But I get an error :

test.mm:11 error: redefinition if 'int mystatic'

Why I get this error in ObjC++ and not in ObjC ?

like image 362
TheFrancisOne Avatar asked Jul 05 '12 14:07

TheFrancisOne


1 Answers

This boils down to the difference between C and C++. In C it is OK to redefine a static variable with the same name and the same type; in C++, doing the same is an error.

From the C standard:

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with a storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definitions for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

From C++ standard:

C.1.2, 3.1 Change: C++ does not have “tentative definitions” as in C. E.g., at file scope,

int i ;
int i ;

is valid in C, [but it is] invalid in C++.

As far as Objective C is concerned, the language does not support variables scoped at the class level; declaring static int mystatic; inside an @implementation block has exactly the same effect as declaring it outside the @implementation block. To emulate class-scoped variables, use function-scoped static variables inside class methods.

like image 113
Sergey Kalinichenko Avatar answered Oct 16 '22 11:10

Sergey Kalinichenko