我在 C# 或其他语言中使用过类似的东西。
// C#, but I can't remember correctly. Just assume like a pseudo code.
class A
{
public int b;
public A(int newB)
{
b = newB
}
public static const A a1 = A(1);
public static const A a2 = A(2);
public static const A a3 = A(3);
}
在 C++ 中有类似的东西吗?或者任何推荐的方法来做到这一点?我这样做只是为了在其类名中组织 a1
、a2
、a3
。
最佳答案
Is there equivalent of this in C++?
以下 C++ 代码等同于您的 C# 代码:
class A
{
public:
int b;
A(int newB) : b(newB)
{
}
//declaration
static const A a1;
static const A a2;
static const A a3;
};
//definition - necessary!
const A A::a1 = A(1);
const A A::a2 = A(2);
const A A::a3 = A(3);
关于c++ - 静态数据成员是否可以在 C++ 中键入其容器类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5443119/