在声明C++之后初始化常量变量

initializing const variables after the declaration C++

本文关键字:常量 变量 初始化 之后 声明 C++      更新时间:2023-10-16

我试图在C++中声明一个常量变量;

        #include <iostream>
        #include <pthread.h>
        #include <stdlib.h>
        using namespace std;
        //
        // declare the mutex
        //
        static pthread_mutex_t mutex    = PTHREAD_MUTEX_INITIALIZER;
        //
        // AVOID NEGATIVE NUMBERS
        //
        unsigned int MAXSIZE = 0;
        unsigned int head = 0;
        unsigned int tail = 0;  
        //
        // return a empty circular queue
        //
        int* initialize(int size){
           MAXSIZE = size;
           pthread_mutex_lock( &mutex );
           int* queue = new int[ MAXSIZE ];
           // initialize each position to zero ( explicitly )
           for(int i = 0; i < MAXSIZE; i++){
            queue[i] = 0;
           } 
           pthread_mutex_unlock( &mutex );
           return queue;
        }
        //
        // enqueue number into the queue
        // returns the position it was stored
        //
        void* enqueue( void* local_queue, void* local_data ){
           // ASSERT ONLY ONE THREAD EACH  TIME
           pthread_mutex_lock( &mutex );
           // convert back to int
           int data = *((int*)(&local_data));
           int* queue = (int*)local_queue;
           queue[tail] = data;
           tail = (tail+1) % MAXSIZE;
           pthread_mutex_unlock( &mutex );
           cout << "Tail: " << tail << endl;
        }
        //
        // dequeue, given the queue
        //
        void* dequeue( void* queue ){
           int temp;
           pthread_mutex_lock( &mutex );
           int* local_queue = ( int* )queue; 
           temp = local_queue[ head ];
           head = ( head + 1 ) % MAXSIZE;
           pthread_mutex_unlock( &mutex );
           cout << "Removed: " << temp << endl;
    }
    // 
    // print the queue as it is
    //
    void* display( void* local_queue ){
       pthread_mutex_lock( &mutex );
       int* queue = (int*)local_queue;
       if( head == tail ){
        cout << "Queue underflow" << endl;
       }
       else{
        //
        // prints each element in the queue
        //
        for( unsigned int i = head; i < tail; i = (i+1) % MAXSIZE ){
            cout << queue[i] << endl;
        }
       }
       pthread_mutex_unlock( &mutex );
    }
    //
    // delete the memory allocated to the queue
    //
    void remove( int* queue){
       delete queue;
    }
    //
    // explain the user how to run the program
    //
    void usage(){
      cout << "Usage: " << endl; 
      cout << "    ./queue [size] [elements]" << endl;
      cout << "ex: ./queue 5 0 1 2 3 4 5" << endl;
    }
    //
    // main function, the tests are done in the for loop
    //
    int main( int argc, char* argv[] ){
       pthread_t threads[5];
       if(argc < 2){
        cout << "Args must be at least 1 " << endl;
        usage();
        return -1;
       }
       for(size_t j = 0; j < 5; j++){   
        unsigned int size = atoi( argv[1] );
        cout << "Size: " << size << endl;
        int* queue = initialize(size);
        for(size_t i = 2; i < argc; i++){
            enqueue( queue, (void*)atoi( argv[i] ) );
        }
            pthread_create( &threads[j], NULL, dequeue, (void*)queue );
        // make sure memory is freed 
        // finally end the thread
        pthread_join( threads[j], NULL );
        remove(queue);
       }
       return 0;
    }

我希望将unsigned int MAXSIZE = 0;声明为const unsigned int MAXSIZE;,这样我就可以在runtime初始化它。我知道这可以在class的构造函数中完成,但我想知道是否有一种方法可以将MAXSIZE初始化为用户给定的sizeMAXSIZE用于实现在数组中实现的循环队列,因此将MAXSIZE声明为const非常重要,以避免被更改从而影响队列的circular操作。谢谢*我希望我能充分澄清我的问题,以便得到更准确的答案。为了完整性和社区的利益,我把我所有的代码都添加到了这个问题中*

在运行时初始化const变量的唯一方法是如果它是类的成员。然后可以使用构造函数的初始化列表来设置初始值。

在C++11中,您可以执行此

const extern int i; // declare in *.h file
const int i = [](){ // init
     return 10;
}();

如果必须在运行时"初始化"一个变量,那么它不是常量变量。只要让它变得不恒定,然后忘记这个问题。

只需使用:

const unsigned int MAXSIZE = 1000;

申报后:

extern const unsigned int MAXSIZE;  // THIS is a declaration
const unsigned int MAXSIZE = 1000;

问题是,const unsigned int MAXSIZE不是一个声明,而是一个定义并执行初始化。

AFAIK,只有在声明常量变量时才能初始化它们。

在您的代码中,为什么不直接使用参数size呢?

int* initialize(const unsigned int size){
   pthread_mutex_lock( &mutex );
   // MAXSIZE = size;
   int* queue = new int[ size];
   // initialize each position to zero ( explicitly )
   for(int i = 0; i < size; i++){
        queue[i] = 0;
   }
   pthread_mutex_unlock( &mutex );
   return queue;
}

将const作为参数传递的全部目的是确保它不会在函数内部发生更改。