3

我正在尝试在中声明一个常量变量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用户size给出的方法。MAXSIZE用于实现在数组中实现的循环队列,所以MAXSIZE声明为const避免被更改进而影响circular队列的操作很重要。谢谢。*我希望我能澄清我的问题,以获得更准确的答案。为了完整性和社区的利益,我已将所有代码添加到问题中。*

4

5 回答 5

5

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

于 2012-08-03T01:47:22.167 回答
1

只需使用:

const unsigned int MAXSIZE = 1000;

声明后:

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

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

于 2012-08-03T01:45:30.997 回答
1

AFAIK,您只能在声明 const 变量时对其进行初始化。

在您的代码中,为什么不只使用参数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 作为参数传递的全部目的是确保它不会在函数内部发生变化。

于 2012-08-03T01:48:41.823 回答
1

如果您必须在运行时“初始化”一个变量,它就不是一个常量变量。只需让它变得非常数并忘记这个问题。

于 2012-08-03T01:55:30.220 回答
1

在 C++11 中,你可以做到这一点

const extern int i; // declare in *.h file

const int i = [](){ // init
     return 10;
}();
于 2020-01-22T02:53:07.403 回答