Thursday, June 3, 2010

Static C++

   1: #include <stdio.h>
   2: #include <iostream>
   3:  
   4: int first(void)
   5:   {
   6:       int i = 0; 
   7:  
   8:      return (i++);
   9:  }
  10: int second(void)
  11: {
  12:     static int i = 0;
  13:     return(i++);
  14: }
  15:  
  16:  
  17: int main()
  18: {
  19: int counter;
  20: for(counter = 0;counter <3 ;counter++)
  21: {
  22:     printf("First %d \n", first());
  23:  
  24: }
  25: for(counter = 0;counter <3 ;counter++)
  26: {
  27:     printf("Second %d \n", second());
  28:  
  29: }
  30: std::cin>>counter;
  31:     return (0);
  32:  
  33: }

sonuçlar

first 0 0 0

second 0 1 2

şeklinde.C# da bir fonksiyon içine static değişken tanımlanamazken c++ da tanımlabiliyor ve değer atansa bile sadece ilk oluşturur ken kullanıyor

No comments: