C/C++ changing the value of a const

后端 未结 18 1833
醉梦人生
醉梦人生 2020-11-28 07:28

I had an article, but I lost it. It showed and described a couple of C/C++ tricks that people should be careful. One of them interested me but now that I am trying to repli

18条回答
  •  半阙折子戏
    2020-11-28 07:38

    The article you were looking at might have been talking about the difference between

    const int *pciCantChangeTarget;
    const int ci = 37;
    pciCantChangeTarget = &ci; // works fine
    *pciCantChangeTarget = 3; // compile error
    

    and

    int nFirst = 1;
    int const *cpiCantChangePointerValue = &nFirst;
    int nSecond = 968;
    
    *pciCantChangePointerValue = 402; // works
    cpiCantChangePointerValue = &ci; // compile error
    

    Or so I recall-- I don't have anything but Java tools here, so can't test :)

提交回复
热议问题