C++ template to cover const and non-const method

后端 未结 7 1399
北荒
北荒 2020-12-29 22:48

I have a problem with duplication of identical code for const and non-const versions. I can illustrate the problem with some code. Here are two s

相关标签:
7条回答
  • 2020-12-29 23:34

    Normally with this type of thing, it's possibly better to use methods that make sense. For example, load() and save(). They say something specific about the operation that is to be carried out via the visitor. Typically both a const and non-const version is provided (for things like accessors anyway), so it only appears to be duplication, but could save you some headache debugging later down the line. If you really wanted a workaround (which I wouldn't advice), is to declare the method const, and all the members mutable.

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