Jump to content
  • Advertisement
Sign in to follow this  
AussieSpoon

Using a pointer to point to a new object every frame

This topic is 2101 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,

 

I have a Sprite class with a Vector2 pointer (member) that is used for a sprite's position. It's a pointer because I'm forward declaring it's class so it has to be a pointer.

 

So I want to take in a Vector2 (parameter) and then have the pointer point to the Vector taken in without creating a new object every frame and still have it be efficient. (Or is this the way to do it?). . But I'm unsure how to do it. All I can think of is:

 

void Sprite::SetPos(const Vector2 _position)						
{
	delete m_Position;
	m_Position = new Vector2(_position);
}	

 

 

 

But I'm pretty sure it doesn't work (or does it?)

 

Please tell me if that doesn't make sense. 

 

Thanks

 

Share this post


Link to post
Share on other sites
Advertisement

You should definitely consider if you really want to have a single primitive thing like a vector to be a pointer. Why do you only have a forward declaration of the vector and not the full class?

Share this post


Link to post
Share on other sites
You should definitely consider if you really want to have a single primitive thing like a vector to be a pointer. Why do you only have a forward declaration of the vector and not the full class?

For cleanliness reasons (I don't want 100 lines of code before the actual class

 

and I'm trying to use forward declarations so I don't have a whole bunch of #includes (it can slow down compile time, it's just a practice thing for me though) . 

Share this post


Link to post
Share on other sites

There's much much more wrong in that piece of code than just a plain pointer issue.

 

Besides the allocations you do every frame, there are also a lot more copy-constructor calls than you might expect.

Get rid of that; it may be just a Vec2 class but clean code doesnt hurt anyone.  use const references, or just pass the position as 2 parameters.

 

Next point is ... why do you want to use a pointer anyway? Sprite classes usually store their position for collision detection or rendering or whatever.

And you do realize that pointers take up memory as well, right?  Depending on the OS, the size is different. On Windows it's 4 byte if i remember correctly.

So basically you win nothing using your method.

 

best regards

Share this post


Link to post
Share on other sites
There's much much more wrong in that piece of code than just a plain pointer issue.

 

Besides the allocations you do every frame, there are also a lot more copy-constructor calls than you might expect.

Get rid of that; it may be just a Vec2 class but clean code doesnt hurt anyone.  use const references, or just pass the position as 2 parameters.

 

Next point is ... why do you want to use a pointer anyway? Sprite classes usually store their position for collision detection or rendering or whatever.

And you do realize that pointers take up memory as well, right?  Depending on the OS, the size is different. On Windows it's 4 byte if i remember correctly.

So basically you win nothing using your method.

 

best regards

 

true. And talking about efficiency. Malloc/new are quite expensive in terms of computation cost (if you compare it to just decreasing the stack pointer) and also lead to heap fragmentation. 

Share this post


Link to post
Share on other sites
You should definitely consider if you really want to have a single primitive thing like a vector to be a pointer. Why do you only have a forward declaration of the vector and not the full class?

For cleanliness reasons (I don't want 100 lines of code before the actual class

 

and I'm trying to use forward declarations so I don't have a whole bunch of #includes (it can slow down compile time, it's just a practice thing for me though) . 

 

Trading run-time performance away for better compile-tile performance is just a bad optimization to make. Bad, bad, bad. Don't do that. 

Share this post


Link to post
Share on other sites
Note that you can still use assignment on an object that you have a pointer to (assuming the assignment operator wasn't made non-public or otherwise inaccessible). In your case you can do *m_Position = _position; which would update the value without requiring a new allocation.

Share this post


Link to post
Share on other sites
I'm lost...
//vector2.hpp
#ifndef VECTOR2_HPP
#define VECTOR2_HPP

class vector2{
//stuff...
};

#endif
//sprite.hpp
#ifndef SPRITE_HPP
#define SPRITE_HPP

#include "vector2.hpp"

class sprite{
private:
vector2 m_position;
};

#endif

What am I missing here? Edited by MarkS

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!