[英]How to set a static member variable in the .cpp file using anonymous namespace?
I have a class that contains a static member variable, I would like to initialize it using an anonymous namespace in the .cpp file just like what I saw in the link : Where to put constant strings in C++: static class members or anonymous namespaces 我有一个包含静态成员变量的类,我想在.cpp文件中使用匿名名称空间对其进行初始化,就像在链接中看到的那样: 在C ++中的何处放置常量字符串:静态类成员或匿名名称空间
But I am getting an error saying the current member rate cannot be defined in the scope. 但是我收到一个错误,说当前会员费不能在范围内定义。 Why? 为什么?
//A.h
namespace myclass
{
class A
{
private:
static double rate;
};
}
//A.cpp
namespace myclass
{
namespace{
double A::rate = 99.9;
}
}
You can't: it's already a qualified member of a class: 您不能:它已经是课程的合格成员:
//A.cpp
namespace myclass
{
double A::rate = 99.9;
}
will do. 会做。 The static
will already stick, because of the declaration. 由于声明,该static
变量将一直存在。
The confusion might be because static
has different meanings: 造成混淆的原因可能是因为static
具有不同的含义:
However, a static
class member doesn't have anything to do with visibility (internal/external linkage). 但是, static
类成员与可见性 (内部/外部链接)无关。 Instead it has to with storage duration . 相反,它必须具有存储期限 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.