Does it make sense to provide non-const reference getter

前端 未结 9 896
滥情空心
滥情空心 2020-12-16 23:58

Sometimes I need to expose some of the class members. For example in the following example class Mechanic may need direct access to Engine componen

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 00:20

    They can be useful when the value you are returning is actually on the heap.

    template class Singleton
    {
    private:
        static T* m_pSingleton;
    
    public:
        T& getSingleton() { assert(m_pSingleton); return(*m_pSingleton); };
    
    }; // eo class Singleton
    

提交回复
热议问题