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'指向一个不相关的对象。 –