6

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 ?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
TheFrancisOne
  • 2,667
  • 9
  • 38
  • 58
  • 1
    +1 Good question! The error is correct (`static` is file scoped, not class scoped) but why the Objective-C compile doesn't catch is beyond me... – trojanfoe Jul 05 '12 at 14:12
  • OK, so, is there a way to declare a static member class scoped ? – TheFrancisOne Jul 05 '12 at 14:19
  • Not in Objective-C no, other than using different names for each variable. – trojanfoe Jul 05 '12 at 14:20
  • 2
    I do not understand why would anyone downvote this question: I think it is an entirely legitimate and very interesting question, stated in clear unambiguous terms. – Sergey Kalinichenko Jul 05 '12 at 14:32
  • Note that Objective-C has neither "member variables", "static members" nor "class variables". While "member variables" and "instance variables" are nearly identical, it is helpful to stick to the vocabulary of the language. – bbum Jul 05 '12 at 16:33

1 Answers1

6

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523