Uninitialized constant members in classes

前端 未结 3 511
心在旅途
心在旅途 2021-01-04 17:50

This is probably a ridiculously easy question, but I\'ve been searching around for the answer for a while yet can\'t seem to figure this out. I\'m trying to initialize a con

相关标签:
3条回答
  • 2021-01-04 18:22

    Your code works for me (MSVC2010) - as I believe it should. What compiler are you trying this with?
    The only complaint a compiler may/should have with the code is a warning that the automatic copy constructor and assignment operator cannot be created because of the const member.

    0 讨论(0)
  • 2021-01-04 18:31

    You must initialize your constant member in the initialization list of ALL constructors. You are doing it only for the one with an argument. Do it for the default one too, and everything will be fne. In this particular case, either initialize your thresh with 0, or disable the default constructor.

    0 讨论(0)
  • 2021-01-04 18:46

    The problem is in the default constructor, it should be

    Scheduler::Scheduler() : thresh(0) {}
    

    or not be implemented at all.

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