正確には、なぜB b = (B&) a
コンパイルして動作するのにB b = (B) a
、以下のプログラムでは動作しないのですか?
#include <iostream>
using namespace std;
class A {public: void f(){ cout<<"A"<<endl;} };
class B : public A { public: void f(){cout<<"B"<<endl;} };
void g(A a){ B b = (B&) a; b.f(); }
int main() {
B b; g(b);
return 0;
}
ここで欠落している参照を使用して派生型にキャストすることについて何かありますか? B にキャストすると、コンストラクター B(A a) が存在しないというコンパイル時エラーが発生します。