CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    Quote Originally Posted by monarch_dodra View Post
    In Paul's words:"Learn about virtual functions". If you don't understand, it means you don't have a correct understanding about virtual inheritance. If you learn about them, it will make sense.

    Sorry for the kinda harsh answer, but sometimes you need to stop to understand before rushing too far.
    oh on it's not harsh at all xD i am reading about them

  2. #17
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    after doing some reading i understand move is a default function, for me it's just random movement BUT in zombie, man and hunter the move will be specific/unique to them.


    this means that i will have to define :

    virtual bool IsHumanInRange(Body &);
    virtual void Hunt(Body &);

    in the body to but change in the hunter and badger

    i hope i am right xD
    Last edited by Mariusmssj; April 16th, 2010 at 08:15 AM.

  3. #18
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: I want to optimize my code

    Quote Originally Posted by Mariusmssj View Post
    after doing some reading i understand move is a default function, for me it's just random movement BUT in zombie, man and hunter the move will be specific/unique to them.


    this means that i will have to define :

    virtual bool IsHumanInRange(Body &);
    virtual void Hunt(Body &);

    in the body to but change in the hunter and badger

    i hope i am right xD
    That sounds right

  4. #19
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    ok i tried to search and read but i am still bit confused of how to Populate aEnemies with pointers to ALL enemies "/

    Code:
    	int zomb = 5;
    	int hol = 5;
    	int hunt = 2;
    	int range = 20;
    
    	//creates the objects in the game
    	Man a_man;
    	std::vector<Zombie> zombies(zomb);
    	std::vector<Hole> holes(hol);
    	std::vector<Hunter> hunters(hunt);
    
    	std::vector<Body*> aEnemies; //Populate this with pointers to ALL enemies, regardless of type.
    	//This will be your main object
    	std::vector<Body*>::iterator it = aEnemies.begin();
    	std::vector<Body*>::iterator itEnd = aEnemies.end();
    	for( ; it!=itEnd ; ++it )
    	{
        	if (it->isHumanInRange(a_man))
        	{
            	it->hunt(a_man);
        	}
        	else
        	{
            	it->move();
        	}
    	}
    Nut now i understand what it will do I will have a vector FULL of enemies - zombies, hunters badgers and such, and i will have this loop, which will have 2 things, MOVE and HUNT but what is cool that move and hunt will be specific. The move for hunter and zombie will be different, kinda like a overloaded function. Am i right?
    Last edited by Mariusmssj; April 16th, 2010 at 09:04 AM.

  5. #20
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: I want to optimize my code

    Quote Originally Posted by Mariusmssj View Post
    ok i tried to search and read but i am still bit confused of how to Populate aEnemies with pointers to ALL enemies "/

    Code:
        int zomb = 5;
        int hol = 5;
        int hunt = 2;
        int range = 20;
    
        //creates the objects in the game
        Man a_man;
        std::vector<Zombie> zombies(zomb);
        std::vector<Hole> holes(hol);
        std::vector<Hunter> hunters(hunt);
    
        std::vector<Body*> aEnemies; //Populate this with pointers to ALL enemies, regardless of type.
        //This will be your main object
        std::vector<Body*>::iterator it = aEnemies.begin();
        std::vector<Body*>::iterator itEnd = aEnemies.end();
        for( ; it!=itEnd ; ++it )
        {
            if (it->isHumanInRange(a_man))
            {
                it->hunt(a_man);
            }
            else
            {
                it->move();
            }
        }
    Nut now i understand what it will do I will have a vector FULL of enemies - zombies, hunters badgers and such, and i will have this loop, which will have 2 things, MOVE and HUNT but what is cool that move and hunt will be specific. The move for hunter and zombie will be different, kinda like a overloaded function. Am i right?
    yeah, the result is kind of the same as an overload, but conceptually, given it's run time dynamic behavior, it is also completely different.

    Do you understand why you have a vector of pointers to enemies, rather than actual enemies? There are multiple reasons, which involve slicing, static typing, and the fact that virtual calls are only resolved when called through pointers (->) (well, it also works with references, but this is less recommended, from a maintenance point of view).

    Usually, you do something like this:

    Code:
    body* bodyHunter = new hunter
    However, this is rife with potential memory leaks. It is easier to create an auto object, and point to that:

    Code:
    {
        hunter actualHunter
        body* bodyHunter = &actualHunter
        //actualHunter is destroyed here
    }
    This is why you have a vector of Badgers/Zombies etc. It also has the advantage of less calls to new.

    This is how you populate it, I guess.

    Code:
        std::vector<Zombie> zombies(zomb);
    
        std::vector<Body*> aEnemies;
    
        std::vector<Zombie>::iterator it = zombies.begin();
        std::vector<Zombie>::iterator itEnd = zombies.end();
        {
            aEnemies.push_back(&*it);
        }
    ... ditto with Hunters etc.
    &*it means "the address of where it points" This is usually not needed, but sometimes the type of it is not an actual pointer. For example, you always have to do this for lists. With vectors, you can usually get away with using it straight up.

    (I'm sure there are efficient std algorithms to do this, but that's besides the point)

    On a side note, in the above code I had written, the correct code is:
    Code:
        std::vector<Body*>::iterator it = aEnemies.begin();
        std::vector<Body*>::iterator itEnd = aEnemies.end();
        for( ; it!=itEnd ; ++it )
        {
            if ((*it)->isHumanInRange(a_man))
            {
                (*it)->hunt(a_man);
            }
            else
            {
                (*it)->move();
            }
        }
    This is because it is an iterator to a pointer, not an iterator to an object.

  6. #21
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    Quote Originally Posted by monarch_dodra View Post
    yeah, the result is kind of the same as an overload, but conceptually, given it's run time dynamic behavior, it is also completely different.

    Do you understand why you have a vector of pointers to enemies, rather than actual enemies? There are multiple reasons, which involve slicing, static typing, and the fact that virtual calls are only resolved when called through pointers (->) (well, it also works with references, but this is less recommended, from a maintenance point of view).

    Usually, you do something like this:

    Code:
    body* bodyHunter = new hunter
    However, this is rife with potential memory leaks. It is easier to create an auto object, and point to that:

    Code:
    {
        hunter actualHunter
        body* bodyHunter = &actualHunter
        //actualHunter is destroyed here
    }
    This is why you have a vector of Badgers/Zombies etc. It also has the advantage of less calls to new.

    This is how you populate it, I guess.

    Code:
        std::vector<Zombie> zombies(zomb);
    
        std::vector<Body*> aEnemies;
    
        std::vector<Zombie>::iterator it = zombies.begin();
        std::vector<Zombie>::iterator itEnd = zombies.end();
        {
            aEnemies.push_back(&*it);
        }
    ... ditto with Hunters etc.
    &*it means "the address of where it points" This is usually not needed, but sometimes the type of it is not an actual pointer. For example, you always have to do this for lists. With vectors, you can usually get away with using it straight up.

    (I'm sure there are efficient std algorithms to do this, but that's besides the point)

    On a side note, in the above code I had written, the correct code is:
    Code:
        std::vector<Body*>::iterator it = aEnemies.begin();
        std::vector<Body*>::iterator itEnd = aEnemies.end();
        for( ; it!=itEnd ; ++it )
        {
            if ((*it)->isHumanInRange(a_man))
            {
                (*it)->hunt(a_man);
            }
            else
            {
                (*it)->move();
            }
        }
    This is because it is an iterator to a pointer, not an iterator to an object.
    well all the pointers are so you could have all the list of all the enemies and just using that, you could move the specific ones.

    and

    Code:
        std::vector<Zombie> zombies(zomb);
    
        std::vector<Body*> aEnemies;
    
        std::vector<Zombie>::iterator it = zombies.begin();
        std::vector<Zombie>::iterator itEnd = zombies.end();
        {
            aEnemies.push_back(&*it);
        }
    ... ditto with Hunters etc.
    i need a loop here don't i?

  7. #22
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: I want to optimize my code

    I did not understand either question

  8. #23
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    "Do you understand why you have a vector of pointers to enemies, rather than actual enemies?"

    yes i so, because this gives me kinda a list of all the objects which can attack the man. So i could control movement of zombie, hunter and ect. with that one for loop

    and

  9. #24
    Join Date
    May 2009
    Posts
    2,413

    Re: I want to optimize my code

    The purpose of OO is to make future changes easy but I feel the use of the Body superclass rather is convoluting this design now. It's being used for too much. I think it should be limited to handle position and movement only.

    The only property that is unambigiously shared between zombies, hunters, enemies, badgers and whathaveyou in this design is that they have a position and can move. Other relationships are much better handled outside of Body. In short subclasses of Body shouldn't be assumed in Body.
    Last edited by nuzzle; April 16th, 2010 at 01:08 PM.

  10. #25
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    Quote Originally Posted by nuzzle View Post
    The purpose of OO is to make future changes easy but I feel the use of the Body superclass rather is convoluting this design now. It's being used for too much. I think it should be limited to handle position and movement only.

    The only property that is unambigiously shared between zombies, hunters, enemies, badgers and whathaveyou in this design is that they have a position and can move. Other relationships are much better handled outside of Body. In short subclasses of Body shouldn't be assumed in Body.
    so i should re-work it using SRP?

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured