A variable that is read-only after assignment at run-time?

后端 未结 4 986
小蘑菇
小蘑菇 2021-02-06 05:22

Fairly new programmer here, and an advance apology for silly questions.

I have an int variable in a program that I use to determine what the lengths of my a

相关标签:
4条回答
  • 2021-02-06 06:04

    When exactly do you know the correct value? If you read it from a file or whatever, you can just say:

    const int n = determine_correct_value();
    
    0 讨论(0)
  • 2021-02-06 06:07

    C++ doesn't have a built-in solution for this, but if you really want to make sure that your int is only assigned once, you can build your own special int class:

    class MyConstInt
    {
    public: 
        MyConstInt(): assigned(false) {}
        MyConstInt& operator=(int v)
        {
            assert(!assigned); 
            value = v; 
            assigned = true; 
            return *this; 
        }   
        operator int() const 
        { 
            assert(assigned); 
            return value; 
        }
    private: 
        int value; 
        bool assigned; 
    }; 
    
    
    MyConstInt mi; 
    //  int i = mi;         //  assertion failure; mi has no value yet
    mi = 42; 
    //  mi = 43;        //  assertion failure; mi already has a value
    int* array = new int[mi]; 
    
    0 讨论(0)
  • 2021-02-06 06:08

    I'm tempted to say that what you want doesn't make sense. A constant is something that doesn't change its value, not something that maybe changes its value once or twice. If you want a global variable, just make it non-constant.

    On the other hand, if you have scope-constant values, you would just declare and initialize them at the same time, following the general C++ guideline to declare as close to the usage site as possible. For example, mark the use of constants in the following local scope:

    for (auto it = v.begin(), end = v.end(); it != end; ++it)
    {
      const Foo & x = *it;
      const std::size_t n = x.get_number_of_bars();
    
      // use x and n ...
    
      const bool res = gobble(x, zip(n));
      if (res && shmargle(x)) { return 8; }
    }
    

    Here the compiler may even choose not to generate any special code for the variables at all if their value is already known through other means.

    0 讨论(0)
  • 2021-02-06 06:11

    I'd use a function-static variable and a simple function. Observe:

    int GetConstValue(int initialValue = 0)
    {
      static int theValue = initialValue;
      return theValue;
    }
    

    Since this is a function-level static variable, it is initialized only the first time through. So the initialValue parameter is useless after the first run of the function. Therefore, all you need to do is ensure that the first call of the function is the one that initializes it.

    0 讨论(0)
提交回复
热议问题