Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4596258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:05:22+00:00 2026-05-21T23:05:22+00:00

In C++ if a variable is declared as a static variable inside a class

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:05:23+00:00Added an answer on May 21, 2026 at 11:05 pm
    // 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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't
If a variable is declared as static in a function's scope it is only
I have a boolean variable declared at the top of a class and when
In VB.Net, I can declare a variable in a function as Static, like this:
What are the benefits of having a member variable declared as read only? Is
I've seen some variable declare in VB.net in several way like: print(dim _Foo as
Let's say I have the following simple table variable: declare @databases table ( DatabaseID
In C# and in Java (and possibly other languages as well), variables declared in
I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at
I declare a variable for a 64 bit counter as : long long call_count;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.