I'm getting unexpected behavior from the following code:
struct Base
{
Base() {}
virtual ~Base() {}
virtual void foo() const = 0;
protected:
Base(const Base &) {}
};
struct Derived : public Base
{
Derived() {}
Derived(const Derived &other) : Base(other) {}
virtual void foo() const {}
};
struct NewDerived
{
operator const Derived() { return Derived(); }
};
void func(const Base &b)
{
b.foo();
}
int main()
{
func(NewDerived());
return 0;
}
With MSVC2008, I get this compilation error in main():
error C2248: 'Base::Base' : cannot access protected member declared in class 'Base'
Why is it trying to access the copy constructor of Base?
If I make Base's copy constructor public, the code compiles and slices the return value at runtime and the call to foo() inside func() triggers a pure virtual function called error.
Can someone please shed a bit of light?