0
struct Base{};
struct Derived: Base{};
int main() {
Derived *d{};
Base *&b = d; // Error: non-const reference to rvalue.
(void)b;
return 0;
}
正如評論指出,分配試圖存儲非const引用到右值。我知道右值是d
轉換爲Base *
。但是,如何解決此問題,因此可以通過分配b
更改d
?
我懷疑你想要:'Derived d; Base&b = d;' –
@PiotrSkotnicki不,我真的想要指針。 – AlwaysLearning
如果允許,你可以說'b = new Base;'。 'd'指向一個不相關的對象。 –