Overiding 'helper' functions

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 509 Views - Last Post: 22 April 2013 - 03:11 PM Rate Topic: -----

#16 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 20 April 2013 - 02:43 PM

Ah, I thought you were talking about the delete vs erase. Yes, using the while loop fixes a problem with the for loop.
This threw me off:

Quote

You could use erase, but it's a terribly inefficient way of deleting the elements.

Do you have a better way to remove elements from a vector?
Was This Post Helpful? 0
  • +
  • -

#17 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Overiding 'helper' functions

Posted 20 April 2013 - 02:49 PM

Quote

Do you have a better way to remove elements from a vector?


Yeah, just relying on the deconstructor. An erase involves (vector.size - 1) - (index + 1) copies. The deconstructor will remove all elements and will not involve any copies.

That's the whole point of using a vector isn't? Avoiding explicit memory management :smile2: .

This post has been edited by jjl: 20 April 2013 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 20 April 2013 - 02:55 PM

But the deconstructor applies to the entire vector. I'm talking about removing one object out of many while leaving the rest intact. Assume you have a model of people and one of them dies. You want to remove that one from the vector and continue with your simulation. The only way I see to do that is to delete the dynamically created object and then erase the pointer held in the vector.

This post has been edited by CTphpnwb: 20 April 2013 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#19 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Overiding 'helper' functions

Posted 20 April 2013 - 02:56 PM

Quote

But the deconstructor applies to the entire vector. I'm talking about removing one object out of many while leaving the rest intact. Assume you have a mode of people and one of them dies. You want to remove that one from the vector and continue with your simulation. The only way I see to do that is to delete the dynamically created object and then erase the pointer held in the vector.

Yes, it's the only option in the case (or you could just set it to NULL). But in the original case of just deleting all the elements, relying on the deconstructor is a better choice.

I tend not to use erase because of it's linear complexity.

This post has been edited by jjl: 20 April 2013 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

#20 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 20 April 2013 - 03:00 PM

Well, in the original case I wouldn't really care because all memory is freed when the code ends. I was just trying to show an example of how to delete both the object(s) and the pointer(s).
;)
Was This Post Helpful? 0
  • +
  • -

#21 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Overiding 'helper' functions

Posted 20 April 2013 - 03:12 PM

I want the Bullet_TrackingMissile class to have access to the x and y coordinates of the enemy it is tracking, so it can update its path every frame. But im having issues figuring out how to do it. My first attempt looked like this:
#pragma once

#include "Bullet.h"

class Bullet_TrackingMissile : public Bullet
{
private:
	Enemy* target;
public:
	Bullet_TrackingMissile (sf::Texture& spriteSheet, Enemy& tar, int x, int y,int range)
	{
		target = &tar;
		...

	}

	void Update()
	{
		//here target's x and y values are processed to update angles and positions of the bullet
	}
};




This doesnt work though because target is tar as it was when it was passed. Im trying to do something like
Enemy enemy1();
enemy1.setX( 7 );

Bullet.SetEnemy(enemy1);
Bullet.GetEnemyX(); // this should return enemy1's X, 7

enemy1.setX( 8 );

Bullet.GetEnemyX(); // this should return enemy1's X, 8. HOWEVER i cant figure out how to do this. I know it has        
                    // something to do with pointers



Was This Post Helpful? 0
  • +
  • -

#22 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 20 April 2013 - 03:16 PM

View Postjjl, on 20 April 2013 - 05:56 PM, said:

I tend not to use erase because of it's linear complexity.

I'm getting a bit off topic, but:

I think that there are times when erase is cost effective when compared to setting the pointer to null. Imagine a simulation starting with a million people where there are births and deaths. If you set pointers to null when they die the vector will grow with every birth and never shrink. Since it's a simulation there must be interactions between people, so you need to iterate through the entire vector for every time cycle. At some point, the number of null pointers in the vector would out number the living, possibly by a wide margin. Better to remove them and keep the number reasonable. To keep it as efficient as possible, you might do this all at once at the end of a time cycle. That would require keeping track of deaths as they occur and then deleting them starting with the last death and working towards the first.

This post has been edited by CTphpnwb: 20 April 2013 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#23 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 20 April 2013 - 08:00 PM

View PostNano511, on 20 April 2013 - 06:12 PM, said:

This doesnt work though because target is tar as it was when it was passed. Im trying to do something like
Enemy enemy1();
enemy1.setX( 7 );

Bullet.SetEnemy(enemy1);
Bullet.GetEnemyX(); // this should return enemy1's X, 7

enemy1.setX( 8 );

Bullet.GetEnemyX(); // this should return enemy1's X, 8. HOWEVER i cant figure out how to do this. I know it has        
                    // something to do with pointers



  • Look at my example code. See how I have a vector that contains pointers to objects? See how you're not doing that?
  • It would be good to know what your SetEnemy() method looks like.

Was This Post Helpful? 0
  • +
  • -

#24 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Overiding 'helper' functions

Posted 20 April 2013 - 08:03 PM

Quote

I'm getting a bit off topic, but:

I think that there are times when erase is cost effective when compared to setting the pointer to null. Imagine a simulation starting with a million people where there are births and deaths. If you set pointers to null when they die the vector will grow with every birth and never shrink. Since it's a simulation there must be interactions between people, so you need to iterate through the entire vector for every time cycle. At some point, the number of null pointers in the vector would out number the living, possibly by a wide margin. Better to remove them and keep the number reasonable. To keep it as efficient as possible, you might do this all at once at the end of a time cycle. That would require keeping track of deaths as they occur and then deleting them starting with the last death and working towards the first.

If you are having to shift data around all the time (erase), it's usually a sign of using the wrong data structure. In your example, I'd probably use a tree since it fit's the problem naturally and has constant complexity for most operations. :winkiss:
Was This Post Helpful? 0
  • +
  • -

#25 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 21 April 2013 - 06:02 AM

That might work, but I'm not sure it would be worth it. People aren't dying all the time and even when they do you're only moving the pointers, not the data. Using a tree you'd end up managing at least two child pointers and one parent pointer. That seems like a lot of complexity for a potentially small gain.

By the way:

Nano511, this side discussion may seem off topic but it is all relevant to what you're trying to do. You should pay attention and ask questions if you don't understand!

This post has been edited by CTphpnwb: 21 April 2013 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#26 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Overiding 'helper' functions

Posted 21 April 2013 - 09:20 AM

View PostCTphpnwb, on 20 April 2013 - 08:00 PM, said:

[*]Look at my example code. See how I have a vector that contains pointers to objects? See how you're not doing that?
[*]It would be good to know what your SetEnemy() method looks like.


Yeah all of that was psuedo code because i dont know how to write the actual code. It was just so you could understand what im trying to do. The first part of that post contained real code though, but it diddnt do what i wanted. Im not sure what you were saying about vectors because the code you quoted didn't contain vectors. I dont thing I can explain what im trying to do any better than i did with that pseudocode. Here it is again with some explanations. This is what i want to happen. I don't actually know how to do this. You can see what i attempted in one of my previous posts.

Enemy enemy1();   // An enemy object (enemy1) is created
enemy1.setX( 7 ); // it's X variable is set to 7.

Bullet.SetEnemy(enemy1); // Here Bullet is given some kind of reference to enemy1, stored in a variable named target
Bullet.GetEnemyX();      // This is to prove the reference was received.

enemy1.setX( 8 );        // X varable set to 8 so that we can see if it changed target's X, because target references 
                         // enemy1
 
Bullet.GetEnemyX(); // This is to prove that when enemy1 is changed, target is also changed because target references
                    // enemy1. It should return 8, since enemy1's X was set to 8 and target references enemy1. 



Was This Post Helpful? 0
  • +
  • -

#27 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,178
  • Joined: 08-August 08

Re: Overiding 'helper' functions

Posted 21 April 2013 - 02:18 PM

View PostNano511, on 21 April 2013 - 12:20 PM, said:

View PostCTphpnwb, on 20 April 2013 - 08:00 PM, said:

[*]Look at my example code. See how I have a vector that contains pointers to objects? See how you're not doing that?
[*]It would be good to know what your SetEnemy() method looks like.

Im not sure what you were saying about vectors because the code you quoted didn't contain vectors.

Huh? From post #10:
int main(int argc, const char * argv[])
{

	vector <shape*> test;

It's all about this vector!

View PostNano511, on 21 April 2013 - 12:20 PM, said:

Enemy enemy1();   // An enemy object (enemy1) is created
enemy1.setX( 7 ); // it's X variable is set to 7.

Bullet.SetEnemy(enemy1); // Here Bullet is given some kind of reference to enemy1, stored in a variable named target
Bullet.GetEnemyX();      // This is to prove the reference was received.

enemy1.setX( 8 );        // X varable set to 8 so that we can see if it changed target's X, because target references 
                         // enemy1
 
Bullet.GetEnemyX(); // This is to prove that when enemy1 is changed, target is also changed because target references
                    // enemy1. It should return 8, since enemy1's X was set to 8 and target references enemy1. 



I'm not sure what you're trying to do there, but it seems to me that you should have a list (an array or vector, perhaps?) of targets and a position/speed/direction for a bullet. If, as it moves, that bullet's position intersects with any target then you have a hit on the target.
Was This Post Helpful? 0
  • +
  • -

#28 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,574
  • Joined: 19-February 09

Re: Overiding 'helper' functions

Posted 22 April 2013 - 03:11 PM

The data not updating suggests the you might have made a copy of an Enemy object at some point. Not explored here.


#include <iostream>
#include <string>

using namespace std;

class Enemy
{
  public:
  string name;
  int count;
  Enemy() {name = "Bob"; count = 0;}

  int  getCount() {return count;}
  void update() {count++;}
  void display() {cout << name << " : " << count << endl;}
};

class Tracker
{
  public:
  Enemy *target;
  Tracker() {target = NULL;}

  void setEnemyRef(Enemy &e) {target = &e;}
  void setEnemyPtr(Enemy *e) {target = e;}
  int getTargetData() {return target->getCount();}
};


int main()
{
  Enemy enemy1;
  Tracker tracker1;

  tracker1.setEnemyPtr(&enemy1);

  for(int i=0; i<5; i++) {
    enemy1.update();
    cout << tracker1.getTargetData() << endl;
  }

  cout << "---" << endl;
  
  Enemy enemy2;
  Tracker tracker2;

  tracker2.setEnemyRef(enemy2);

  for(int i=0; i<5; i++) {
    enemy2.update();
    cout << tracker2.getTargetData() << endl;
  }

  return(0);
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2