【发布时间】:2017-10-05 13:06:02
【问题描述】:
如果我像这样使用mutable 和const 指针:
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;
为什么编译器在第二种情况下会报错?
【问题讨论】:
-
错误信息是什么意思?它可能值得一读。
-
编辑您的问题以包含构建错误。完整且完整地逐字复制粘贴为文本。
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及重现它所需的最短代码在问题本身。
标签: c++ pointers constants mutable