是否可以從動態轉換獲取對基類指針的引用?從dynamic_cast獲取非const引用
#include <cassert>
class A{
public:
virtual ~A(){}
};
class B : public A{};
int main(){
A *a = new B;
B *&b = dynamic_cast<B *>(a);
// work with pointer as of type B
b = new B; // this should change a aswell
assert(a == b);
}
這個代碼不被錯誤invalid initialization of non-const reference of type 'B*&' from an rvalue of type 'B*'