如果我使用mutable
與const
指針這樣的:可變在C++常量指針
class Test
{
public:
mutable const int* ptr; // OK
};
它的正常工作。
但是,如果我用這樣的:
class Test
{
public:
mutable int * const ptr; // Error
};
錯誤:
prog.cpp:6:25: error: const 'ptr' cannot be declared 'mutable'
mutable int * const ptr;
^
prog.cpp: In function 'int main()':
prog.cpp:11:7: error: use of deleted function 'Test::Test()'
Test t;
^
prog.cpp:3:7: note: 'Test::Test()' is implicitly deleted because the default definition would be ill-formed:
class Test
^
prog.cpp:3:7: error: uninitialized const member in 'class Test'
prog.cpp:6:25: note: 'int* const Test::ptr' should be initialized
mutable int * const ptr;
爲什麼編譯器給在第二種情況下的錯誤?
是什麼錯誤消息說?這可能值得一讀。 – VTT
@VTT http://ide.geeksforgeeks.org/PWB4ti – Jayesh
*編輯您的問題*以包含構建錯誤。作爲文本逐字複製粘貼,全部完整。 –