Give Foo a constructor which takes a Base parameter:
class Foo : public Base {
public:
explicit Foo(const Base &b) {
// do work to initialise Foo. You might want to include
Base::operator=(b);
// or you might not. Depends how Foo works, and what Base's
// constructors do.
}
};
Whenever you write a single-argument constructor, you should consider whether or not you want to specify "explicit".
"explicit" is there to say that the constructor should only be used where you've explicitly written a constructor call or a cast. If it's not there, then the compiler will also "implicitly" convert Base objects to Foo, for example in the following situation:
int doSomething(const Foo &f) {
return 23;
}
Base b;
doSomething(b); // doesn't compile
doSomething(static_cast<Foo>(b)); // does compile
If you removed the "explicit", then doSomething(b)
would compile, and do the same as the second line - construct a temporary Foo object from b, and pass that to doSomething.