Is there some ninja trick to make a variable constant after its declaration?

后端 未结 8 1392
广开言路
广开言路 2020-12-01 09:13

I know the answer is 99.99% no, but I figured it was worth a try, you never know.

void SomeFunction(int a)
{
    // Here some processing happens on a, for ex         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 09:20

    this might be one way to do it, if you are just trying to avoid another name. i suggest you think twice before using this.

    int func ()
    {
        int a;
        a %= 10;
    
    const int const_a = a;
    #define a const_a
    
        a = 10;  // this will cause an error, as needed.
    #undef a
    }
    

提交回复
热议问题