我不明白这个片段的行为:(用 clang++ 3.0 编译)
#include <iostream>
using namespace std;
class Base {
public:
virtual void bar() {}
bool foo = false;
};
class Derived : public Base{
public:
Derived() { Base::foo = true; }
};
int main() {
Derived d;
Base b(d);
cout << b.foo << endl; // prints 0
// prints 1 if "virtual void bar() {}" is removed
}
为什么函数 Base::bar() 对 Base::foo 的复制有任何影响?