Using a pointer to point to a new object every frame

Started by
15 comments, last by Khatharr 11 years, 3 months ago

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

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?

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) .

Managed to figure it out by creating the object in the classes constructor, thanks

Forward declarations should be used if you only need a pointer or reference to minimize includes. Making every member a pointer and basically screwing up your design, just so you can use forward declarations isn't going to make your code cleaner. The consequences will be far messier than just having that include. Are you deleting the object behind the pointer in the destructor? Did you create a copy constructor and assignment operator to prevent crashes from deleting it twice (or at least make your class noncopyable)? That's a lot of extra code and effort just to be able to avoid that one include.

I'm also not sure how it's supposed to save you code. Whether it's "include xxx" or "class xxx", you are going to have this one line of code.

f@dzhttp://festini.device-zero.de

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

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.

I open sourced my C++/iOS OpenGL 2D RPG engine :-)



See my blog: (Tutorials and GameDev)


[size=2]http://howtomakeitin....wordpress.com/

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.

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.
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?

This topic is closed to new replies.

Advertisement