c++将变量重新定义为常量
我有一个结构:
struct s { UINT_PTR B_ID; }; s d; d.B_ID=0x1;
效果很好,但我希望 d.B_ID 保持不变。我尝试使用 (const) 但没有成功。因此,在我为 d.B_ID 赋值后,我想将其设为常量。
有什么想法吗?
EDIT
好吧,我不希望整个结构是一个常量。
当我设置计时器并使用 b.B_ID 作为计时器的想法时。
在 开关(wparam) { case b.B_ID // 错误:B_ID 必须是常量 .... 休息; } 所以这就是为什么我需要它成为一个常数
I have a struct:
struct s { UINT_PTR B_ID; }; s d; d.B_ID=0x1;
That works fine, but I want d.B_ID to be constant. I tried to use (const) but it didn't work. So after I put a value to d.B_ID, then I want make it a constant.
Any ideas?
EDIT
ok i don't want the whole struct a constant.
when i set timer and use the b.B_ID as an idea for the timer.
in the
switch(wparam)
{
case b.B_ID // error: B_ID must be constant
....
break;
}
so that is why i need it to be a constant
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
变量修饰符在编译时为每个变量固定。您可能需要解释您正在尝试做的事情的背景,但这也许会满足您的需求?
由于您使用的是 C++,我建议:
来完成此操作
Ramiz Toma
:我需要使用 s.B_ID=something在 C/C++ 类型修饰符(如 const)中 在运行时为给定类型声明,并且不能在运行时更改。这意味着,如果变量被声明为 const,则永远不能使用赋值运算符对其进行赋值。它只会在构造时被赋值。但这不是问题,因为您始终可以通过适当的设计来解决这个问题。
如果您说需要使用赋值,我认为这是因为您在知道变量的值是什么之前创建了结构。如果是这种情况,那么您只需移动结构声明,直到知道该值为止。
例如,
这将不起作用,因为如果您希望 b.D_ID 是“不可分配的”,它将始终如此。您将需要重构您的代码,类似于:
Variable modifiers are fixed at compile time for each variable. You may have to explain the context of what you are trying to do, but perhaps this will suit your needs?
Since you are using C++ I would recommend:
Ramiz Toma
: well i need way to do it using the s.B_ID=somethingIn C/C++ type modifiers (like const) are declared at run time for a given type and cannot be changed at run time. This means that if a variable is declared
const
it can never be assigned to using the assignment operator. It will only be assigned a value when it is constructed.This is not a problem however because you can always get around this by proper design.
If you say you need to use assignment, I assume that this is because you create the struct before you know what the value of the variable will be. If this is the case then you simply need to move the struct declaration till after you know the value.
For example
This will not work, because if you want b.D_ID to be 'un assignable' it will always be so. You will need to refactor your code similarly to:
编辑:抱歉,这是 C++ 中更新的代码片段
EDIT: Sorry, here is the updated code snippet in C++
在 C++ 语言中,
case
标签必须从积分常量表达式 (ICE) 构建。 ICE 是编译器在错误消息中的术语“常量”下暗示的含义。类的非静态成员不能在 ICE 中使用。从字面上做你想做的事情是不可能的。即不可能在case
标签中使用结构成员。在这种情况下忘记
switch
/case
吧。使用普通的if
分支而不是switch
语句。In C++ language the
case
label must be built from an Integral Constant Expression (ICE). ICE is what the compiler implies under the term "constant" in your error message. A non-static member of a class cannot be used in an ICE. It is not possible to do literally what you are trying to do. I.e. it is not possible to use a struct member in acase
label.Forget about
switch
/case
in this context. Use ordinaryif
branching instead ofswitch
statement.你不能那样做 - 即。不可能有选择地将结构体的单个成员设置为 const。一种选择是“构造”整个结构:
或者您可以在构造时设置它:
You can't do that - ie. it is not possible to selectively make a single member of a struct const. One option is to 'constify' the entire struct:
Or you could set it at construction:
另一种方法是 getter 和一次性 setter
Another way would be a getter and a one time setter