使用过多的静态是好还是坏? [英] Is using too much static bad or good?
问题描述
我喜欢在 C++ 中使用静态函数作为对它们进行分类的一种方式,就像 C# 那样.
I like to use static functions in C++ as a way to categorize them, like C# does.
Console::WriteLine("hello")
这是好是坏?如果经常使用这些函数我想没关系,但如果不经常使用它们会不会对内存造成压力?
Is this good or bad? If the functions are used often I guess it doesn't matter, but if not do they put pressure on memory?
static const
怎么样?
推荐答案
是好是坏
想到的第一个形容词是不必要的".C++ 有自由函数和命名空间,为什么你需要在类中将它们设为静态函数?
The first adjective that comes to mind is "unnecessary". C++ has free functions and namespaces, so why would you need to make them static functions in a class?
在 C# 和 Java 中的不可实例化类中使用静态方法是一种解决方法,因为这些语言没有自由函数(即,函数直接驻留在命名空间中,而不是作为一部分一个班级).C++ 没有这个缺陷.只需使用命名空间.
The use of static methods in uninstantiable classes in C# and Java is a workaround because those languages don't have free functions (that is, functions that reside directly in the namespace, rather than as part of a class). C++ doesn't have that flaw. Just use a namespace.
这篇关于使用过多的静态是好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!