Hello.
Currently i am doing some testing using polymorphism, and i got stuck.
I cannot type cast, i don't know how to type cast properly it seems. I have bean googling, goes true posts reading and i just cant get it to work.
What i require is a example of code, i know there are bunch of it, its just painfull that i cant find any.
Long story short:
Two classes named, "one" and "two"
class "two" inherits class "one"
in main i have storage for pointer to class "one"
i stored pointer to class "two" in storage for pointers to class "one"
i am trying to pass the stored pointer to class "two" into a function and i just cant figure out how to typecast a (one*) to (two*)
help me please
/////////////////////////////////////////////////////////////////////////////
class one
{
public:
one(int a);
int a;
};
one::one(int a = 0) : a(a)
{std::cout<<"one constructor<<"<<std::endl;}
/////////////////////////////////////////////////////////////////////////////
class two : public one
{
public:
two(int s);
int s;
};
two::two(int s = 0) : s(s)
{std::cout<<"two constructor"<<std::endl;}
/////////////////////////////////////////////////////////////////////////////
void func(two * objTwo);
int main()
{
std::vector<std::unique_ptr<one>> oneList;
oneList.push_back(std::unique_ptr<two>(new two));
func(dynamic_cast<two*>(oneList[0].get()));
system("pause");
return 0;
}
///////////////////////////////////////////////////////////////////////////
void func(two * objTwo)
{
std::cout<<objTwo->a<<" "<<objTwo->s<<std::endl;
}