你可以看看這個例子代碼:返回引用一個const指針爲const類型
class Test {
int *a;
int b;
public:
Test() : b(2)
{
a = new int(5);
}
const int * const& GetA() const
{
const int * const& c = a;
return a;
}
const int& GetB()
{
return b;
}
~Test()
{
delete a;
}
};
我得到回報的警告。爲什麼將一個const引用的引用返回給一個const變量是錯誤的,但是可以返回一個const變量的引用嗎?順便說一句,如果我在GetA()中返回c,它編譯得很好。
你會得到什麼警告? – NathanOliver
無法在VS 2015上重現問題 – alexeykuzmin0
我得到了對臨時[-Wreturn-local-addr]的返回引用。 – lepo