随笔 - 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 小寻 阅读(1196) 评论(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;
}
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: