I have the following code to print the derived structure variable in a global function. But when I tried to compile the code, g++ returns the following error. Is it not possible to typecast the structure to a derived class from base class which is passed to the function by pass by value?
In function 'void print(B)':
Line 19: error: no matching function for call to 'D1::D1(B&)'
Code:
struct B{
int a;
B()
{
a = 0;
}
};
struct D1:public B{
std::string s1;
D1()
{
s1.assign("test");
}
};
void print(B ib)
{
cout << static_cast<D1>(ib).s1<<endl;
}
int main()
{
D1 d1;
cout << d1.s1 <<endl;
print(d1);
return 0;
}