Definition of static member in objective-C and objective-C++
Asked Answered
I

1

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 ?

Insulate answered 5/7, 2012 at 14:6 Comment(5)
+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...Backhander
OK, so, is there a way to declare a static member class scoped ?Insulate
Not in Objective-C no, other than using different names for each variable.Backhander
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.Stableman
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.Cresida
S
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.

Stableman answered 5/7, 2012 at 14:19 Comment(2)
+1 So persumably if -Wall is used, this will be noticed by the developer?Backhander
@Backhander No, -Wall does not warn you about a second definition, because it is not against the rules spelled out in the standard.Stableman

© 2022 - 2024 — McMap. All rights reserved.