Angus
Angus

Reputation: 12621

Could not able to get the same behaviour of static variable in c++ with objective C

In C++ if a variable is declared as a static variable inside a class and if its variable is changed by the inherited subclass then while accessing the variable with the super class object will give the changed value.

In Objective C we are declaring the static variable as we do in the C declaration.

So here the variable accessed by the derived class object has the separate copy of the variable and the same variable though accessed by the base class is not giving the value that is changed by the derived class object.

What should I change so that the change made by the derived class to the variable ab will also get reflected while accessing it through the base class?

ClassA.h
-----------

#import <Foundation/Foundation.h>

static int ab;

@interface ClassA : NSObject {

        int a;

}

+ (void) initialize;
- (id) init;
- (void) displayNumOfInstance;
- (void) disp;

@end


ClassA.m
------------

#import "ClassA.h"


@implementation ClassA

+ (void) initialize
{
        ab=0;
}

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

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

- (void) disp
{
  NSLog(@"The value is %d",ab);
}

@end

ClassB.h
--------

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

@interface ClassB : ClassA {


}

- (void) display;

@end


ClassB.m
--------

#import "ClassB.h"


@implementation ClassB

- (void) display
{
    ab=20;
    NSLog(@"The value ab is %d",ab);
}

@end


class2.m
---------

#import <Foundation/Foundation.h>
#import "ClassA.h"
int main (int argc, const char * argv[]) {

        ClassA *a = [[ClassA alloc]init];
        [a disp];
        [a release];


       ClassB *b = [[ClassB alloc]init];
       [b display];
       [b release];

      ClassA *a1 = [[ClassA alloc]init];
      [a1 disp];
      [a1 release];

      ClassB *b1 = [[ClassB alloc]init];
      [b1 display];
      [b1 release];

    return 0;
}

output:
---------

2011-04-30 15:31:42.490 class2[1674:a0f] 30
2011-04-30 15:31:42.493 class2[1674:a0f] The value ab is 20
2011-04-30 15:31:42.494 class2[1674:a0f] 30
2011-04-30 15:31:42.495 class2[1674:a0f] The value ab is 20

Upvotes: 0

Views: 102

Answers (1)

Alexander N.
Alexander N.

Reputation: 564

// ClassA.h
@interface ClassA : NSObject { }

+ (void)setFoo:(int)aFoo;
+ (int)foo;

@end

// ClassA.m
static int foo;

@implementation ClassA

+ (int)foo { return foo; }
+ (void)setFoo:(int)aFoo { foo = aFoo; }

@end

// ClassB.h
#import "ClassA.h"
@interface ClassB : ClassA { }
@end

// ClassB.m
#import "ClassB.h"
@implementation ClassB
@end

// main
ClassA *a = [[ClassA alloc] init];
[[a class] setFoo:42];
NSLog(@"%d", [[a class] foo]);

ClassB *b = [[ClassB alloc] init];
[[b class] setFoo:265];
NSLog(@"%d", [[a class] foo]);

Console log:

2011-05-06 20:52:07.092 test[17417:207] 42
2011-05-06 20:52:07.094 test[17417:207] 265

Is this a behaviour you want to achieve?

Upvotes: 1

Related Questions