Manually destroy a glm::vec3?

Hi,

Quick question! Is it possible to manually destroy a glm::vec3. I’ve tried calling a destuctor with v.glm::~vec3();, but I get a Qualified member access refers to a member in namespace ‘glm’ error.

I want to call the destructor because I’ve written a simple, custom data structure - similar to an std::vector - and I want to implement a .clear() method.

Any ideas?

Thanks.

If you have it in your data structure like:

  glm::vec3 v; 

Then you wouldn’t really destroy it because its not a pointer.
Also generally you don’t call destructors on objects. The idea is if a class has a destructor it is called automatically as needed.

If you wanted to reset the value though you could do:

 v = {0,0,0};

Hope that helps.

I have a glm::vec3* data array of a certain length. The array is a pointer, but the stored values should be stack-allocated?

The issue with this is that it sets each vector to (0, 0, 0), which makes it hard to distinguish between real, zero vectors, and “cleared” ones.

I’m looking for a way to clear my data structure and I want to implement something like pop back, which would omits the last element from the data structure or array. Both kind of need to destroy elements on demand, and can’t wait for them running out of scope, if that makes sense.

Thank you for your reply.

I would honestly suggest using a vector of shared_ptr for something like this.
Then you can check if the item in the vector is allocated or not.

ie:

vector < shared_ptr<glm::vec3> > data; 

//adding to the vector 
data.push_back( make_shared<glm::vec3>() ); 

//looping through and checking that an element is set or not
for(  auto & vec : data ){
  if( vec ){
    //that means the vec is allocated 
  }
}

//resetting an element not be allocated
for(  auto & vec : data ){
  if( vec && someOtherCondition ){
     vec.reset(); //release the allocated shared_ptr 
  }
}

If you decide to roll your own vector you could still use shared_ptr<glm::vec3> as the data type instead of glm::vec3 - but I would probably still recommend using a vector for something like this unless you have a really specific reason not to.

3 Likes

Thank you! I’ll consider it, but I’m not too familiar with smart pointers yet. :slight_smile: