比方說,我們有一個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