How to properly access c++/CLI initonly TimeSpan field?

前端 未结 2 1964
广开言路
广开言路 2020-12-06 11:51

The following code compiles with warning and intellisense error:

ref class Test {
    initonly static TimeSpan Delay = TimeSpan(1,1,1); 

    Test() {
               


        
2条回答
  •  半阙折子戏
    2020-12-06 12:08

    Thanks to Hans Passant for his explanation, I understand the context much better now

    I have just come across the same problem.

    My solution was

    • replace the 'initonly' by 'const'
    • When accessing, just cast the const away, and there will not be any errors/warnings left

    Maybe this is not 'state of the art' coding but it works fine, see here (VisualStudio2017 does still expose this behaviour/problem)

提交回复
热议问题