• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: where have all the IVARS gone? (long time passing...)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: where have all the IVARS gone? (long time passing...)


  • Subject: Re: where have all the IVARS gone? (long time passing...)
  • From: Bill Bumgarner <email@hidden>
  • Date: Mon, 10 Jul 2006 00:04:14 -0700


On Jul 9, 2006, at 11:49 PM, Vinay Prabhu wrote:

I am confused,

If I declare any variables under the directive
@interface myClass

@end

Is the scope of iVras are not limited to myClass?

Only if they are surrounded in {}s. I.e.

@interface MyClass
{
   int iVar;
}
@end

Otherwise, you are declaring global variables.

And, unfortunately, you can't add instance variables to a class via a category; this won't work:

@interface MyClass (Private)
{
   int anotherVar;
}
@end


Yes, I am using only one instance of object from myClass. May be because of this, I haven't noticed the problem... :(

....
In the implementation file if I declare few variables like this,

@interface myClass1 (Private)
int iVar;
@end

@interface myClass2 (Private)
int iVar;
@end

What will be the scope of iVar?

Global.

I have tried this, it compiles without any errors!!!
If iVar is global, compiler should have given error for multiple
declaration...

Yeah -- you would think you would get a warning... but you don't!

Given foo.c with contents:

int ivar1;
int ivar1;

int main() {
  return 1;
}

% cc -Wall foo.c
%

No warnings.  That seems like a bug to me.

Consider:

#import <Foundation/Foundation.h>
@interface A:NSObject
+(void)go;
@end
@interface B:NSObject
+(void)go;
@end

@implementation A
@end
@implementation B
@end

@implementation A (private)
int iVar1;
+(void)go
{
  NSLog(@"A: 0x%x", &iVar1);
}
@end

@implementation B (private)
int iVar1;
+(void)go
{
  NSLog(@"B: 0x%x", &iVar1);
}
@end

int main() {
  [A go];
  [B go];
  NSLog(@"main(): 0x%x", &iVar1);
  return 0;
}

The output:

% gcc foo.m -framework Foundation
foo.m:10: warning: incomplete implementation of class `A'
foo.m:10: warning: method definition for `+go' not found
foo.m:12: warning: incomplete implementation of class `B'
foo.m:12: warning: method definition for `+go' not found
% ./a.out
2006-07-10 00:03:51.287 a.out[520] A: 0x30b4
2006-07-10 00:03:51.288 a.out[520] B: 0x30b4
2006-07-10 00:03:51.288 a.out[520] main(): 0x30b4



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: where have all the IVARS gone? (long time passing...)
      • From: Chris Suter <email@hidden>
    • RE: where have all the IVARS gone? (long time passing...)
      • From: Vinay Prabhu <email@hidden>
References: 
 >RE: where have all the IVARS gone? (long time passing...) (From: Vinay Prabhu <email@hidden>)

  • Prev by Date: Core Data entities not getting loaded into MOC when opening file
  • Next by Date: RE: where have all the IVARS gone? (long time passing...)
  • Previous by thread: RE: where have all the IVARS gone? (long time passing...)
  • Next by thread: RE: where have all the IVARS gone? (long time passing...)
  • Index(es):
    • Date
    • Thread