Sembra che il mio problema sia un errore in MSVC. Sto utilizzando Visual Studio 2008 con Service Pack 1 e il mio codice funziona con GCC (come testato su codepad.org).Errore di ereditarietà virtuale in MSVC
Qualche informazione ufficiale su questo bug? Qualche idea su come aggirarla? Il bug è stato risolto in VS2010? Tutti gli approfondimenti sarebbero molto apprezzati.
Il codice:
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's 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;
}
Questo sembra essere un bug. – GManNickG
Lo fa ancora in Visual Studio 2010. – Corey
Ho notato che il codepad utilizza g ++ 4.1.2, quindi ho provato con Borland C++ 5.82 e funziona perfettamente. – Corey