Could you please explain work of static_cast?
Why this code doesn't work?
D another_d = static_cast<D>(br); // ERROR - no matching function for call to ‘D::D(B&)’
My code
#include <bits/stdc++.h>
using namespace std;
struct B {
int m = 0;
void hello() const {
std::cout << "Hello world, this is B!
";
}
};
struct D : B {
void hello() const {
std::cout << "Hello world, this is D!
";
}
};
int main(){
D d;
B br = d; // upcast via implicit conversion
br.hello(); // Hello world, this is B!
// D another_d = static_cast<D>(br); // ERROR - no matching function for call to ‘D::D(B&)’
D& another_d = static_cast<D&>(br); // OK
another_d.hello(); // Hello world, this is D!
}
question from:https://stackoverflow.com/questions/65871998/static-cast-no-matching-function-for-call