Advertisement

Polymorphisam, storing unique_ptr and usage problems.

Started by October 08, 2013 05:28 PM
5 comments, last by BaneTrapper 11 years, 4 months ago

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

dynamic_cast only works with classes that have virtual functions.

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

nvm...

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

Oh, i see i am doing things wrong.

Thanks!

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

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?

This topic is closed to new replies.

Advertisement