static int gCounter;

@implementation Fraction

+(Fractoin *)allocF

{

  extern int gCounter;//由于gCounter定义在该文件中,此行定义可省略;

  ++gCounter;

  return  [Fraction alloc];

}

+(int)count

{

  extern int gCounter;//由于gCounter定义在该文件中,此行定义可省略;

  return gCounter;

}

@end

 

#import "Fraction"

int main (int argc, char *argv[])

{

  @autoreleasepool{

     Fraction *a, *b, *c;

     NSLog(@"Fractions allocted: %i", [Fraction count]);

     a = [[Fraction allocF] init];

     b = [[Fraction allocF] init];

     c = [[Fraction allocF] init];

     NSLog(@"Fractions allocted: %i", [Fraction count]);

  }

  return 0;

}

 

输出:

Fraction allocated:0

Fraction allocated:3

相关文章:

  • 2022-12-23
  • 2021-06-13
  • 2021-09-19
  • 2021-10-31
  • 2022-03-05
  • 2022-02-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2022-02-09
  • 2022-12-23
相关资源
相似解决方案