- 506 名前:デフォルトの名無しさん mailto:sage [2008/04/11(金) 08:50:00 ]
- 素朴な疑問です。
#include <iostream> struct S { virtual void hoge() = 0; // 純粋仮想のみ }; struct S1 : public S { void hoge() { std::cout << "S1" << std::endl; }}; struct S2 : public S { void hoge() { std::cout << "S2" << std::endl; }}; int main() { S1 s1; S2 s2; S& r1 = s1; S& r2 = s2; r1.hoge(); r2.hoge(); r1 = r2; // 基本クラスの参照を代入 r1.hoge(); r2.hoge(); return 0; } VC8で上記のコードを実行すると S1 S2 S1 S2 と表示されました。 r1 = r2;はS::operator=を呼ぶだけなので何も変わらない、と理解したのですが、 これはC++的に正しい挙動なのでしょうか? それとも未定義でたまたまこうなっているだけなのでしょうか?
|

|