创建一个所有成员(属性,函数)都是静态类的(客观)缺点是什么?特别是与使用命名空间相比?或者你更愿意创建全局变量/函数?现代C++中的全局变量
我喜欢创建静态属性,因为我发现它们“更整洁”。 (我确切地知道他们来自哪里,等等)。我对命名空间不是很熟悉。而且我对全局变量不满意,因为我对C关键字不是很熟悉,如extern
和static
。
此外,如果我们考虑类
class MyStaticClass
{
private:
static int x;
static double y;
public:
static float s;
static double weatherForecast(unsigned int, char);
};
和命名空间
namespace MyNamespace
{
int x;
double y;
float s;
double weatherForecast(unsigned int, char);
}
是否有打电话
MyStaticClass::weatherForecast
并呼吁MyNamespace::weatherForecast
之间的差异(性能明智)?阅读/书写
MyStaticClass::s
与阅读/书写MyNamespace::s
之间是否存在差异(表现方面)?如果使用类而不是主类型,上述问题的答案是否会发生变化?
_“或者你愿意创建一个命名空间吗?”_我认为这是今天公认的最佳做法。但这可能只是一个意见。 –
你为什么需要它们?一组常量? + https://isocpp.org/wiki/faq/coding-standards#global-vars –
https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables,这可能是有用的。 – 88877