简体   繁体   中英

initializing const variables after the declaration C++

I'm trying to declare a constant variable in 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;
    }

I would like to have unsigned int MAXSIZE = 0; declared as const unsigned int MAXSIZE; So I could initialize that at runtime . I know it can be done in the constructor of class , however I would like to know whether there is a way to initialize MAXSIZE to size which is given by the user. MAXSIZE is used to implement the circular queue implemented in an array was shown, so it's important to MAXSIZE to be declared as const to avoid being changed and then affect the circular operation of the queue. Thanks. * I hope I clarify my question enough to have a more precise answer. I've added all my code into the question for completeness and for the sake of the community. *

The only way you can initialize a const variable at run-time is if it's a member of a class. Then you can use the initialization list of the constructor to set the initial value.

Just use:

const unsigned int MAXSIZE = 1000;

After the declaration:

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

The thing is, const unsigned int MAXSIZE isn't a declaration, but a definition and performs initialization.

AFAIK, you can only initialize const variables when you declare them.

In your code, why not just use the argument 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;
}

The whole purpose of passing a const as argument, is to ensure it won't get change inside the function.

If you have to "initialize" a variable at run time, it is not a constant variable. Just make it non-constant and forget about this problem.

In C++11, u can do this

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

const int i = [](){ // init
     return 10;
}();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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