How to generate a non-const method from a const method?

后端 未结 11 2599
时光取名叫无心
时光取名叫无心 2021-02-08 11:18

While striving for const-correctness, I often find myself writing code such as this

class Bar;

class Foo {
public:
  const Bar* bar() const { /* code that gets          


        
11条回答
  •  自闭症患者
    2021-02-08 11:44

    You can do something like this:

    class Bar;
    
    class Foo {
    public:
      const Bar* bar() const { return getBar(); }
    
      Bar* bar() {
       return getBar();
      }
    
      private:
        Bar* getBar() const {/* Actual code */ return NULL;}
    };
    

提交回复
热议问题