Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Polymorphisam, storing unique_ptr and usage problems.

This topic is 2110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 unsure.png

/////////////////////////////////////////////////////////////////////////////
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;
}

Share this post


Link to post
Share on other sites
Advertisement

The function needs to be a virtual member of class one and overridden in class two, then you won't have to typecast at all.

 

You can't get polymorphic behaviour from a free function (i.e. not a member function), although you can call virtual methods from a free function when passed a pointer to a base class.

Share this post


Link to post
Share on other sites

I know it's a simplified example, but having a default argument for your base class constructor is not good style, at first glance I thought it wouldn't compile, since two::two doesn't call the base class constructor, it default constructs it with the default argument. If that was a real example it would set alarm bells ringing for me.

Share this post


Link to post
Share on other sites

I know it's a simplified example, but having a default argument for your base class constructor is not good style, at first glance I thought it wouldn't compile, since two::two doesn't call the base class constructor, it default constructs it with the default argument. If that was a real example it would set alarm bells ringing for me.

The code is compiled, and it works.

int main()
{
    two objTwo;
    system("pause");
    return 0;
}

Prompt at console is:

one constructor<<
two constructor
press any key to continue...

Are you not mistaken?

i am allowed to do this in class two

two::two(int s = 0) : s(s), one()
//Or
two::two(int s = 0) : s(s)

produce the same result.

 

I can manualy invoke the constructor, but i guess the compiler does it automatically if not issued.

 

The reason is that it has all its parameters with default arguments so it has nothing to complain about id say.

 

EDIT::
What i do not understand if the whole point of polymorphism is virtual functions.

I don't know how to store different types of units in one storage unit, i most be doing it wrong.

Any tutorials, or example codes that you can provide?

Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!