Кажется, что моя проблема - ошибка в MSVC. Я использую Visual Studio 2008 с пакетом обновления 1, а мой код работает с GCC (как проверено на codepad.org).
Любая официальная информация об этой ошибке? Есть идеи, как обойти это? Исправлена ли ошибка в VS2010? Все идеи были бы оценены.
Код:
struct Base {
Base(int i = 0) : i(i) {}
virtual ~Base() {}
virtual Base *clone() const = 0;
protected:
int i;
};
struct A : virtual public Base {
A() {}
virtual A *clone() const = 0;
};
struct B : public A {
B() {}
B *clone() const { return new B(*this); }
/// MSVC debugger shows that 'b' is for some reason missing the Base
/// portion of it object ("Error: expression cannot be evaluated")
/// and trying to access 'b.i' causes an unhandled exception.
///
/// Note: This only seems to occur with MSVC
B(const B &b) : Base(b.i), A() {}
};
void foo(const A &elem) {
A *a = elem.clone();
if (a) delete a;
}
int main() {
A *a = new B;
foo(*a);
delete a;
}