In the following code although the instances of subclass are getting pushed on the stack of base class but while retrieving(top operation) an element and storing it in derieved class object, the compiler is generating an error.
#include <cstdio>
#include <stack>
using namespace std;
class base{};
class sub : public base{
public:
int a;
sub (int A){ a=A;}
};
int main(){
stack<base> st;
sub A = sub(10);
sub B = sub(20);
st.push(A);
st.push(B);
printf("%d\n", (int)st.size());
sub C = (sub)st.top();
return 0;
}
Error:
In function ‘int main()’:
24:22: error: no matching function for call to ‘sub::sub(base&)’
24:22: note: candidates are:
11:2: note: sub::sub(int)
11:2: note: no known conversion for argument 1 from ‘base’ to ‘int’
8:7: note: sub::sub(const sub&)
8:7: note: no known conversion for argument 1 from ‘base’ to ‘const sub&’
Please suggest any way by which I can typecast the retrieved base class object to derieved class.