B=D;
D=B;
First line will always compile. The second line may or may not compile, as it entirely depends on how you have written each class. The rest of the answer will shed light on it. Read on.
is it so that D=B; will only be valid if B has a default constructor?
No.
D=B
will be valid only if D has defined operator=
which takes B
as argument.
Derived & operator=(const Base &base); //member of Derived
Base B;
Derived D;
D = B; //allowed - assignment
Or if you do that in the initialization of D
, then it will valid only if a constructor of D
takes B
as argument.
Derived(const Base &base); //a constructor of Derived
Base B;
Derived D = B; //allowed - initialization
Or B
has defined a user-defined conversion to D
.
operator Derived(); //member of Base
Base B;
Derived D = B; //allowed - initialization
D = B; //also allowed - assignment