在C++中,您现在可以拥有mutable
成员。这为语言增加了一层“逻辑常量”。这些与只读数据有何关系 - 将会有一个mutable
成员阻止将一个const类放入.rodata
节中?修改const对象的可变成员是否有效?
class Foo {
mutable int bar;
public:
Foo(): bar(0) {}
void set(int x) const { bar = x; }
};
// Can this be in a read-only section?
const Foo foo;
int main(void)
{
// Is this well-defined?
foo.set(5);
}
这就是'mutable'是O_O你似乎已经知道被发现。 – BartoszKP
是的。还有什么可以'可变'工作? – Deduplicator
为什么逻辑上只有const的对象位于'.rodata'节中? –