代码之家  ›  专栏  ›  技术社区  ›  Thomas Eding

C同一函数的常量/非常量版本

  •  2
  • Thomas Eding  · 技术社区  · 14 年前

    假设在C中有函数

    type* func (type*);
    const type* func_const (const type*);
    

    所以它们都有完全相同的内部逻辑。

    2 回复  |  直到 14 年前
        1
  •  10
  •   Jim Buck    14 年前

    const type* func_const (const type*)
    {
        /* actual implementation goes here */
    }
    
    type* func (type* param)
    {
        /* just call the const function where the "meat" is */
        return (type*)func_const(param);
    }
    
        2
  •  2
  •   R.. GitHub STOP HELPING ICE    14 年前

    像标准的C库函数一样,只接受常量限定的参数,同时返回非常量限定的结果(看到了吗 strchr , strstr

    推荐文章