简体   繁体   English

如何初始化全局const指针?

[英]how to initialize a global const pointer?

I have a variable which is the head to a linked list. 我有一个变量,它是链表的头。 I want to make it const because it should never be changed, the variable is used throughout the program, so I thought I should make it a global const. 我想把它变成const因为它永远不应该改变,变量在整个程序中使用,所以我认为我应该把它变成一个全局const。 The problem is that I couldn't initialize it after I declared it const. 问题是我在声明const之后无法初始化它。

How can I get around this problem? 我怎样才能解决这个问题?

typedef struct PT {
 int x;
 int y;
 struct PT *next;
} POINT;

//globals
POINT * const mypoint_head;

int main(int argc, char *argv[])
{
    int size = 100;
    mypoint_head= InitPoint(size);   // error C2166: l-value specifies const object
    //rest of code

}


POINT* InitPoint(int size)
{
   POINT *tmp;
   POINT *orig;
   int a = 10;
   int b = 1000;
   orig = (POINT*) malloc (sizeof(POINT) * size);
   if(orig == NULL)
      return NULL;

   tmp = orig;
   for (i = 0; i < size; i++)
   {
      tmp->x = a++;
      tmp->y = b++;
      if (i == size -1) {
            tmp->next = NULL:
      }
      else {
            tmp->next = tmp+1; 
      }
      tmp++;
   }
   return orig;
} 

你不能 - 这就是const

You are correct in that the variable declared const can never be changed. 你是正确的,因为声明const的变量永远不会改变。 Unfortunately, your mypoint_head= InitPoint(size); 不幸的是,你的mypoint_head= InitPoint(size); line counts as trying to change the variable. 行计为尝试更改变量。 You have to initialize the const variable with a value when it is declared. 在声明时,必须使用值初始化const变量。

Try something like this instead: 尝试这样的事情:

//globals
static POINT head_of_list;
POINT* const mypoint_head = &head_of_list;

Now, you can initialize the list using: 现在,您可以使用以下命令初始化列表:

mypoint_head->next= InitPoint(size-1);

The head-of-list object was declared statically, so it always exists and you eill need to adjust your InitPoint parameters appropriately. 列表头对象是静态声明的,因此它始终存在,您需要适当调整InitPoint参数。 You can also have an extern reference to the pointer in another file without having to make the object it points to directly accessible (for what it's worth). 您还可以对另一个文件中的指针进行extern引用,而不必将其指向的对象直接访问(为了它的价值)。

Nobody has suggested this yet: 还没有人提出这个建议:

int main(int argc, char *argv[])
{
    int size = 100;

    // cast address of mypoint_head to a non-const pointer:
    POINT ** nc_pmh = (POINT **)&mypoint_head;
    // use the address to set mypoint_head:
    (*nc_pmh) = InitPoint(size);
    //rest of code
}

This may not work in C++, where it may not really supply space for const objects. 这可能不适用于C ++,它可能无法为const对象提供空间。

BTW: This is not generally good practice. 顺便说一句:这通常不是好的做法。 In this case, however, it works out well. 然而,在这种情况下,它运作良好。

BTW: you'll want to check the return from InitPoint() , and act accordingly (call exit(), probably). 顺便说一句:你要检查来自InitPoint()的返回,并采取相应的行动(可能是调用exit())。

Remove the const qualifier from the global declaration and declare rest_of_code as a function that takes the const-qualified version of the pointer. 从全局声明中删除const限定符,并将rest_of_code声明为一个采用const限定版本指针的函数。

//globals
POINT * mypoint_head;

void rest_of_code(POINT* const mypoint_head)
{
    mypoint_head = NULL;    // this errors out now
}
int main(int argc, char *argv[])
{
    int size = 100;
    mypoint_head= InitPoint(size);   // no longer errors out
    //rest of code
    rest_of_code(mypoint_head);
}

don't have a global const pointer as your interface to everything else. 没有全局const指针作为其他所有东西的接口。

use a function :- 使用功能: -

static POINT * mypoint_head;

POINT* point_head()
{
    return mypoint_head;
}

You have to use an initializer in the declaration: 您必须在声明中使用初始化程序:

static POINT mypoint_data[100];
POINT * const mypoint_head = &mypoint_head;

Then change your InitPoint function to take a pointer to the dataspace instead of calling malloc and pass it mypoint_data 然后更改您的InitPoint函数以获取指向数据空间的指针,而不是调用malloc并将其传递给mypoint_data

You can also stick extern POINT * const mypoint_head; 你也可以坚持extern POINT * const mypoint_head; in a header file so its accessable in other compilation units. 在头文件中,因此可以在其他编译单元中访问。

(assumes c) const have to be initialized when they are declared and they cannot be evaluated . (假设c)const必须在声明时初始化,并且无法进行求值。 Maybe you could use a const pointer and make it point to your head and expose it to the rest of the code. 也许您可以使用const指针并使其指向您的头部并将其暴露给其余代码。 If i have to achieve may be i will expose the variable by a getListHead() function and give it some obscure name :) 如果我必须实现可能是我将通过getListHead()函数公开变量并给它一些晦涩的名字:)

Wonders what the use case is though. 想知道用例是什么。 I would make my list work even if its head pointer changes. 即使它的头指针改变,我也会使我的列表工作。 (if i have a const head, if i have to delete the head node i will have to shift the elements like in an array) (如果我有一个const头,如果我必须删除头节点,我将不得不像数组一样移动元素)

I can't believe that no one has yet suggested const_cast.. It works just as fine for constant pointers. 我无法相信没有人建议使用const_cast ..它对于常量指针也同样好。

const_cast<POINTER *>(mypoint_head) = InitPoint(size);

Simple as that, no extra variable declaration, nothing. 很简单,没有额外的变量声明,没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM