Problema com destructor em C++

Ace-_Ventura

Power Member
Estou aqui com 1 problema que não consigo fazer a menor ideia do que se passa.
Classes (omiti os ifndef e includes):
Código:
namespace vec {
    
    class Vec3f {
    
    private:
        float _x;
        float _y;
        float _z;
    
    public:
        Vec3f(float x, float y, float z); 
        ~Vec3f(void);
        void operator-=(const Vec3f &vec);
        void operator+=(const Vec3f &vec);
        void normalize(void);
        void crossProduct(const Vec3f &vec);
		float getX() const;
		float getY() const;
		float getZ() const;
		void  setX(float);
		void  setY(float);
		void  setZ(float);
    };
}



Código:
class Entity
{
public:
	vec::Vec3f _position;
public:
	Entity(vec::Vec3f);
	Entity(void);
	~Entity(void);
	vec::Vec3f getPosition(void);
	void setPosition(vec::Vec3f);
};

vec::Vec3f Entity::getPosition(void)
{
	return this->_position;
}
Código:
typedef struct heightmap_t {
        unsigned int width; // map width
        unsigned int length; // map length
        float * heights; // pointer to heights data
        float * colors; // pointer to colors data
        float * normals; // pointer to normals data
} heightmap_t;

typedef struct heightmap_t * heightmap_p;

class HeightMap
{
private:
	heightmap_p _map;
	float _scale; // Z-axis heightmap scale
	GLuint DL;
public:
	HeightMap(char *, float);
	~HeightMap(void);
	unsigned int getWidth(); // get heightmap's width
	unsigned int getLength(); // get tga image's height in pixels
	float getHeight(int x, int z); // get the heightmap heigth on (x, z)
	GLuint getDL();
private:
	GLuint createDL();
};
Código:
class Map  :
	public Entity, public HeightMap
{
private:
	heightmap_p _map;
	float _scale; // Z-axis heightmap scale
public:
	Map(char *, vec::Vec3f, float);
	Map(char *path, float);
	~Map(void);
};

O problema é que quando chamo o getPosition o destructor do Vec3f é sempre chamado. Exemplo:
Código:
Map *m = new Map("data/maps/map.tga", 10.0); 
           m->getPosition();
O destructor é chamado 1 vez.

Código:
glTranslated(m->getPosition().getX(), m->getPosition().getY() , -m->getPosition().getZ());
Aqui o construtor é chamado 3 vezes. Alguma ideia do que se passa?

edit: já testei com outra class de vectores e dá o mesmo problema, pelo que vem de outro lado
 
Última edição:
Poderá ser por estares a retornar uma copia em vez de uma referencia no GetPosition?

Tens isto assim e parece-me que por cada chamada é retornada uma copia de this->_position;
Código:
vec::Vec3f Entity::getPosition(void)
{
    return this->_position;
}
 
Back
Topo