c++将变量重新定义为常量

发布于 2024-09-14 19:19:19 字数 328 浏览 7 评论 0原文

我有一个结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凌乱心跳 2024-09-21 19:19:19

变量修饰符在编译时为每个变量固定。您可能需要解释您正在尝试做的事情的背景,但这也许会满足您的需求?

struct s
{
  int* const B_ID;
};

int main (void) {
  int n = 5;
  s d = {&n};
  int* value = d.B_ID; // ok
  //  d.B_ID = &n; // error
  return 0;
}

由于您使用的是 C++,我建议:

class s {
public:
    int* const B_ID;

    s (int* id) :
    B_ID (id) {
    }
};

void main (void) {
   int n = 5;
   s my_s_variable = s(&n);

   int* value = my_s_variable.B_ID; // ok
   //my_s_variable.B_ID = &n; // error
   return 0;
}

来完成此操作

Ramiz Toma我需要使用 s.B_ID=something在 C/C++ 类型修饰符(如 const)中 在运行时为给定类型声明,并且不能在运行时更改。这意味着,如果变量被声明为 const,则永远不能使用赋值运算符对其进行赋值。它只会在构造时被赋值。

但这不是问题,因为您始终可以通过适当的设计来解决这个问题。

如果您说需要使用赋值,我认为这是因为您在知道变量的值是什么之前创建了结构。如果是这种情况,那么您只需移动结构声明,直到知道该值为止。

例如,

s d; //variable declaration

//calculate B_ID
//...
int* n = 5;
//...


d.B_ID = &n;

这将不起作用,因为如果您希望 b.D_ID 是“不可分配的”,它将始终如此。您将需要重构您的代码,类似于:

//calculate B_ID
//...
int* n = 5;
//...


s d (&n);
//good

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?

struct s
{
  int* const B_ID;
};

int main (void) {
  int n = 5;
  s d = {&n};
  int* value = d.B_ID; // ok
  //  d.B_ID = &n; // error
  return 0;
}

Since you are using C++ I would recommend:

class s {
public:
    int* const B_ID;

    s (int* id) :
    B_ID (id) {
    }
};

void main (void) {
   int n = 5;
   s my_s_variable = s(&n);

   int* value = my_s_variable.B_ID; // ok
   //my_s_variable.B_ID = &n; // error
   return 0;
}

Ramiz Toma: well i need way to do it using the s.B_ID=something

In 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

s d; //variable declaration

//calculate B_ID
//...
int* n = 5;
//...


d.B_ID = &n;

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:

//calculate B_ID
//...
int* n = 5;
//...


s d (&n);
//good
雨后彩虹 2024-09-21 19:19:19
struct s
{
   s() : B_ID(0){}
   UINT_PTR const B_ID;
};
int main(){
   s d;
   d.B_ID=0x1;  // error
}

编辑:抱歉,这是 C++ 中更新的代码片段

struct s
{
   s(UINT_PTR const &val) : B_ID(val){}
   UINT_PTR const B_ID;
};
int main(){
   s d(1);
   d.B_ID=0x1;  // error
}
struct s
{
   s() : B_ID(0){}
   UINT_PTR const B_ID;
};
int main(){
   s d;
   d.B_ID=0x1;  // error
}

EDIT: Sorry, here is the updated code snippet in C++

struct s
{
   s(UINT_PTR const &val) : B_ID(val){}
   UINT_PTR const B_ID;
};
int main(){
   s d(1);
   d.B_ID=0x1;  // error
}
浅唱々樱花落 2024-09-21 19:19:19

在 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 a case label.

Forget about switch/case in this context. Use ordinary if branching instead of switch statement.

骑趴 2024-09-21 19:19:19

你不能那样做 - 即。不可能有选择地将结构体的单个成员设置为 const。一种选择是“构造”整个结构:

s d;
d.B_ID=0x1;
const s cs = s; // when using this B_ID won't be modifiable - but nor would any other members

或者您可以在构造时设置它:

struct s
{
    s(UINT_PTR const p): B_ID(p) {}
    UINT_PTR const B_ID;
};    
s d(0xabcdef);

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:

s d;
d.B_ID=0x1;
const s cs = s; // when using this B_ID won't be modifiable - but nor would any other members

Or you could set it at construction:

struct s
{
    s(UINT_PTR const p): B_ID(p) {}
    UINT_PTR const B_ID;
};    
s d(0xabcdef);
难如初 2024-09-21 19:19:19

另一种方法是 getter 和一次性 setter

class s
{
private:
   bool m_initialized;
   UINT_PTR m_value;
public:
   s() : m_initialized(false), m_value(NULL) {}
   s(UINT_PTR value) : m_initialized(true), m_value(value) {}

   //no need for copy / assignment operators - the default works

   inline UINT_PTR GetValue() const { return m_value; } //getter

   bool SetValue(UINT_PTR value) //works only one time
   {
       if (m_initialized)
       {
          m_value = value;
          m_initialized=true;
          return true;
       }
       else
       {
          return false;
       }
   }

   inline bool IsInitialized() const { return m_initialized; }
};

Another way would be a getter and a one time setter

class s
{
private:
   bool m_initialized;
   UINT_PTR m_value;
public:
   s() : m_initialized(false), m_value(NULL) {}
   s(UINT_PTR value) : m_initialized(true), m_value(value) {}

   //no need for copy / assignment operators - the default works

   inline UINT_PTR GetValue() const { return m_value; } //getter

   bool SetValue(UINT_PTR value) //works only one time
   {
       if (m_initialized)
       {
          m_value = value;
          m_initialized=true;
          return true;
       }
       else
       {
          return false;
       }
   }

   inline bool IsInitialized() const { return m_initialized; }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文