隨筆 - 303  文章 - 883  trackbacks - 0
          <2007年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(357)

          我管理的群

          公共blog

          • n維空間
          • Email : java3d@126.com 群 : 12999758

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網站

          搜索

          •  

          最新評論

          有兩個代碼 第一個是朋友code兄寫的 在他的啟發下,我發現自己的錯誤 寫了下面那個
          寫struct或class 有兩個地方需要注意:
          第一、不能直接對成員變量附值,可以在成員函數或者構造函數里附值
          比如
          class T{int a = 5;};//這樣是錯的

          class T
          {
          public:
              
          int a;
              T(
          void)//構造函數
              {
                a 
          = 5;
              }

              
          void init(void)//成員函數
              {
               a 
          = 5;
              }

          };
          是OK的

          第二、數組一般不能使用a[]或者a[][],
          比如
          class T
          {
             
          int n ;
             
          int m ;
             
          int a[n][m]//提示你不能在class使用不定大小的數組
              int aa[5][6]//除非你這樣用
          };
          要定義不定長的數組,可以這樣
          class T
          {
             
          int *a//提示你不能在class使用不定大小的數組
              initArray(int m,int n)
              
          {
                a 
          = new int[m];
              }

              
          //用完記得釋放,避免野指針
               relArray()
              
          {
                delete []a;
              }

          }
          ;

          //代碼一(code兄寫的)

          #include<iostream.h>

          class Test
          {
          public:
              
          int n;
              int m;
              int **i;
              
          void init(int m,int n)
              
          {
                  
          this->= m;
                  
          this->= n;
                  
                  i
          =new int*[m];
                  
                  
          for(int j=0;j<m;j++)
                  
          {
                      i[j]
          =new int[n];
                  }

              }

              
          void del(int m,int n)
              
          {
                  
          for(int jj=0;jj<m;jj++)
                  
          {
                      delete []i[jj];
                      i[jj]
          =NULL;
                  }

              }

          }
          ;

          int main(void)
          {
              
          int i,j;
              Test 
          *test=new Test;
              test
          ->init(5,6);
              
              
          for(i = 0;i<test->m;i++)
              
          {
                  
          for(j = 0;j<test->n;j++)
                  
          {
                      test
          ->i[i][j] = i+j;
                  }

              }

              
              
          for(i = 0;i<test->m;i++)
              
          {
                  
          for(j = 0;j<test->n;j++)
                  
          {
                      cout
          <<test->i[i][j]<<" ";
                  }

                  cout
          <<endl;
              }

              test
          ->del(5,6);
              
          return 0;
          }


          //代碼二(偶寫滴)

          #include<iostream.h>

          class Test
          {
          public:
              
          int m;
              
          int n;
              
          int *i;
              
          int *j;

              
          void init(int m,int n)
              
          {
                  
          this->= m;
                  
          this->= n;
              }

              
          void build()
              
          {
                  i 
          = new int[this->m];
                  j 
          = new int[this->n];
              }

              
          void del()
              
          {
                  delete []i;
                  delete []j;
              }


          }
          ;

          int main(void)
          {
              
          int i,j;
              Test 
          *test=new Test;
              test
          ->init(5,6);
              test
          ->build();
              
              
          for(i = 0;i<test->m;i++)
              
          {
                  
          for(j = 0;j<test->n;j++)
                  
          {
                      test
          ->i[i] = i+j;
                      test
          ->j[j] = i+j;
                  }

              }

              
              
          for(i = 0;i<test->m;i++)
              
          {
                  
          for(j = 0;j<test->n;j++)
                  
          {
                      cout
          <<test->i[i]<<" ";
                      cout
          <<test->j[j]<<" ";
                  }

                  cout
          <<endl;
              }

              test
          ->del();
              
          return 0;
          }


          地震讓大伙知道:居安思危,才是生存之道。
          posted on 2007-12-15 01:29 小尋 閱讀(1215) 評論(3)  編輯  收藏 所屬分類: c/c++/C#/pasic/vb/php/asp(.net)/win-cgi/xml...

          FeedBack:
          # re: C++class里的動態維數組定義 2007-12-16 17:08 zzzlfr
          TKS!  回復  更多評論
            
          # re: C++class里的動態維數組定義 2007-12-21 11:18 幻想~@@~
          動態數組最好用vector實現 比較方便 下面是我寫的簡單代碼   回復  更多評論
            
          # re: C++class里的動態維數組定義 2007-12-21 11:20 幻想~@@~
          Reference: http://book.csdn.net/bookfiles/17/1001758.shtml

          #include<iostream.h>
          #include
          <vector>
          using std::vector;

          int main()
          {
              vector
          <int> *vi = new vector<int>;//聲明vi里裝的是int變量類型的是數據
              int i = 0;
              
              
          while(i<25)
              
          {
                 vi
          ->push_back(i);
                 i
          ++;
              }


              i 
          = 0;
              
          while(i<25)
              
          {
                 cout
          <<vi->at(i)<<" ";
                 i
          ++;
              }

              
              
          if(!vi)
              
          {
                  delete vi;
              }


              cout
          <<endl;

              
          return 0;
          }
            回復  更多評論
            
          主站蜘蛛池模板: 绿春县| 吉林省| 西青区| 方正县| 怀来县| 裕民县| 天镇县| 阜南县| 育儿| 大埔区| 安阳市| 武汉市| 孙吴县| 兴安盟| 大厂| 秀山| 梅州市| 平原县| 读书| 名山县| 宜君县| 泰兴市| 濮阳县| 合作市| 永泰县| 达孜县| 满城县| 莱州市| 保山市| 和田县| 中阳县| 齐齐哈尔市| 巫山县| 清镇市| 金塔县| 陆河县| 天等县| 洪江市| 赤壁市| 临汾市| 榆社县|