V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fyyz
V2EX  ›  C

C++ 的 class 里创建的构造函数怎么把局部的二维数组升级到 class 内部的全局二维数组?

  •  
  •   fyyz · 2015-11-26 16:13:20 +08:00 · 884 次点击
    这是一个创建于 3075 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码片段:

    class Block
    {
      Block(int width)
      {
        int number_list[width][width]={{0,},};
      }
    
      void text_print()
      {
        for(auto i:number_list)
          {
            std::cout << i << ' ';
          }
      }
    }
    

    我在创建 class 以后,直接使用构造函数创建了一个数组 number_list ,这个数组是要在整个 class 里面到处都用的,但是构造函数本身不能用 return ,不知道如何把 number_list 变成全局变量,方便其他函数使用?

    我尝试过 static ,但是编译器报错,因为我这个数组的长度本身是运行时才确定的。

    错误:‘ number_list ’的存储大小不是常量
         static int number_list[width][width]={{0,},};
    
    14 条回复    2015-11-26 17:16:12 +08:00
    hyq
        1
    hyq  
       2015-11-26 16:21:42 +08:00
    template<int Width>
    class Block
    {
    int number_list[Width][Width];
    }

    这个得在编译期确定大小。
    hxsf
        2
    hxsf  
       2015-11-26 16:22:24 +08:00
    静态的数组变量 number_list 创建的时候根本不知道 width 是多少,所以报错。

    正常做法不是应该定义一个类元素(或者叫类属性)
    然后构造函数里面把外部传进来的数组赋值给自己内部的数组么
    xujif
        3
    xujif  
       2015-11-26 16:22:37 +08:00
    class Block
    {
    protected:
    int **number_list;
    Block(int width)
    {
    this->number_list = new [width][width];
    }
    ~Block()
    {
    delete[] this->number_list
    }


    }
    cfans1993
        4
    cfans1993  
       2015-11-26 16:26:00 +08:00
    如果没理解错的话,你是想把 number_list 作为成员变量使用,那就声明在类里就好了
    class Block{
    ...
    private int **number_list; //int number_list[10][10]
    }
    然后在构造函数 Block(int width)中初始化
    另外数组好像要在定义时指定长度,如果需要动态指定长度的话要用指针,然后在构造函数中 new 一下
    linux40
        5
    linux40  
       2015-11-26 16:27:53 +08:00
    为什么不使用标准库里面的 std::array(编译时确定大小)或 std::vector 或 std::unique_ptr , c++不同于 c ,不能在运行时确定数组大小。
    fyyz
        6
    fyyz  
    OP
       2015-11-26 16:45:29 +08:00
    @cfans1993 数组是可以在运行时指定长度的,不然 main(int argc,char *argv[]) 是怎么起作用的?
    liberize
        7
    liberize  
       2015-11-26 16:45:57 +08:00 via iPhone
    如果用类模板需要 width 是常量,如果用二级指针需要把 width 用成员变量保存下来,后面遍历的时候使用
    fyyz
        8
    fyyz  
    OP
       2015-11-26 16:49:07 +08:00
    @xujif 编译不过去
    错误: expected type-specifier before ‘[’ token
    this -> number_list = new [width][width];
    liberize
        9
    liberize  
       2015-11-26 16:51:01 +08:00 via iPhone
    @fyyz 不要在栈上创建变长数组,另外 char *argv[] 等价于 char **argv ,跟这个没有关系
    liberize
        10
    liberize  
       2015-11-26 16:52:05 +08:00 via iPhone
    @fyyz 明显少了 int 啊,建议楼主回去看看书
    fyyz
        11
    fyyz  
    OP
       2015-11-26 16:55:06 +08:00
    @liberize 加了 int 也不行,报错说 array size in new-expression must be constant 。看来数组长度必须被确定。
    liberize
        12
    liberize  
       2015-11-26 16:56:05 +08:00
    @fyyz 。。。
    liberize
        13
    liberize  
       2015-11-26 16:57:37 +08:00
    GentleSadness
        14
    GentleSadness  
       2015-11-26 17:16:12 +08:00 via Android
    为何不直接在外面搞个数组呢,智能指针不怕 delete
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4993 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:18 · PVG 09:18 · LAX 18:18 · JFK 21:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.