C ++ - 命名空间vs.静态函数 [英] C++ - Namespace vs. Static Functions

查看:144
本文介绍了C ++ - 命名空间vs.静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

命名空间+函数与类上的静态方法

我想将类似的函数分组。我可以做到两种方式之一。对我来说,他们只是语法上的差异...到底是没有关系的。此视图是否准确?

I want to group similar functions togther. I can do it one of two ways. To me they are just syntactical differences...in the end it does not matter. Is this view accurate?

命名空间:

namespace util
  {
  void print_array(int array[])
    {
    int count = sizeof( array ) / sizeof( array[0] );
    for (int i = 0; i <= count; i++) cout << array[i];
    }
  }

类:

class Util
  {
  public:
    static void print_array(int array[])
      {
      int count = sizeof(array);
      for (int i = 0; i <= count; i++) cout << array[i];
      }
  };

使用

Util::print_array()  // Class

util::print_array()  // Namespace


推荐答案

第二种方式是愚蠢和一个完整的废话。类不是命名空间,不应该这样使用。

The second way is silly and a complete nonsense. Classes are not namespaces, and should never be used as such.

此外,代码是错误的, sizeof(array)不会返回您的想法。

Also, the code is wrong, sizeof(array) won't return what you think.

这篇关于C ++ - 命名空间vs.静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆