C++ 中的静态结构
我想定义一个结构,其中存储一些数学常量。
这是我现在得到的:
struct consts {
//salt density kg/m3
static const double gamma;
};
const double consts::gamma = 2350;
它工作正常,但是会有超过 10 个浮点常量,所以我不想在每个浮点常量之前编写“static const”。并定义类似的内容:
static const struct consts {
//salt density kg/m3
double gamma;
};
const double consts::gamma = 2350;
看起来不错,但我遇到了这些错误:
1. 不允许成员函数重新声明
2.非静态数据成员不能在其类之外定义
我想知道是否有任何C++方法可以做到这一点?
I want to define an structure, where some math constants would be stored.
Here what I've got now:
struct consts {
//salt density kg/m3
static const double gamma;
};
const double consts::gamma = 2350;
It works fine, but there would be more than 10 floating point constants, so I doesn't want to wrote 'static const' before each of them. And define something like that:
static const struct consts {
//salt density kg/m3
double gamma;
};
const double consts::gamma = 2350;
It look fine, but I got these errors:
1. member function redeclaration not allowed
2. a nonstatic data member may not be defined outside its class
I wondering if there any C++ way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用命名空间而不是尝试将结构放入命名空间。
访问数据的方法也具有完全相同的语法。例如:
Use a namespace rather than trying to make a struct into a namespace.
The method of accessing the data also has exactly the same synatx. So for example:
听起来你真的只是想要一个命名空间:
除了我会尝试为它想出一个比 consts 更好的名称。
It sounds like you really just want a namespace:
Except I'd try to come up with a better name than
consts
for it.