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.