为什么不能同时用const和static修饰成员函数?
生活随笔
收集整理的這篇文章主要介紹了
为什么不能同时用const和static修饰成员函数?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
const修飾的函數:
表示在該函數體內不能修改成員的值,會在函數中添加一個隱式的參數const this*.
static修飾的函數沒有this指針,與const的用法沖突。
但可以使用static和const修飾成員。
例子如下:
class Singleton
{
public:
static const Singleton* getInstance()
{
return instance;
}
?
Singleton(const Singleton& src) = delete;
Singleton& operator=(const Singleton& src) = delete;
?
private:
Singleton() {}
static const Singleton* instance;
};
const Singleton* Singleton::instance = new Singleton;
總結
以上是生活随笔為你收集整理的为什么不能同时用const和static修饰成员函数?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java飞机大战boss素材_java小
- 下一篇: 伍德里奇计量经济学导论第5版笔记和课后答