比方说,我们有一个test.cpp
如下:为什么我不能声明对可变对象的引用? ( “参考不能声明可变”)
class A;
class B
{
private:
A mutable& _a;
};
编译:
$> gcc test.cpp
test.cpp:6:20: error: reference ‘_a’ cannot be declared ‘mutable’ [-fpermissive]
我的GCC:
$> gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
为什么?
你在找指针吗? –
即使你可以这样做,它也是无用的,因为C++不包含任何可以改变引用的语法。你甚至不能得到参考的内存地址。如果您尝试获取参考指向的对象的地址。 – bames53
对不起,我问了一个错误的问题。这不是一个可变引用,这是对可变对象的引用! (我改了标题) – Martin