[c++] how to use a void * pointer to access the object its pointing to

Hey,

I am using the amazing MSAPhysics addon for my particle simulation. I am running into a problem with probably a couple of solutions but my knowledge of c++ is not good enough to find the right one.

Essentially I need to store custom data inside the particle in one place and extract it in another. The data I want to store in the particle is a reference to a constraint its attached to.

Inside the Physics::Particle Class is a bit which might be just there to do what I want:

  
			  
// custom void* which you can use to store any kind of custom data  
void			*data;  
  

so I store in there the pointer to my constraint:

  
  
  
myparticle->data = constraintPtr;  
  
  

but how do I get the pointer out of the particle in another place and can make c++ believe its of the constraint type for example like this (which obviously won’t work):

  
  
  
Physics2D::Constraint2D * constraint = myparticle->data;  
constraint->kill();  
  
  

Cheers,
Tommi

Hi,

try a static_cast:

  
  
Physics2D::Constraint2D * constraint = static_cast<Physics2D::Constraint2D*>(myparticle->data);    
constraint->kill();   
  

HTH,
Stephan

thanks stephan, works like a charm

T

hey

Actually having a related c++ question. In my example I have different particle groups which have custom properties for example one group’s particle are all white and another one the particles are random colors, all particles are children of one main particle class, for example some have no color info stored others do. For the groups I created likewise different child class of one MainparticleGroup class.

Now my problem is in the MainparticleGroup I have a generic vector group which stores all particles :

  
  
vector<Physics::Particle2D*>            group;  
  

then I have generic draw loop like :

  
  
  
void MSAParticleGroup2D::draw()  
{   
    vector<Physics::Particle2D*>::iterator it = group.begin();  
      
    while( it != group.end() ) {  
        Physics::Particle2D * p = *it;  
        if( p->isDead() ) {  
            it = group.erase(it);  
        } else {  
            ofPushStyle();  
            ofFill();  
            ofSetHexColor( particle_default_color );  
            ofCircle( p->getPosition().x, p->getPosition().y, p->getRadius() );  
            ofPopStyle();  
            it++;  
        }  
    }  
}  
  
  

now the catch is in some of the groups I need to deal with child classes of Physics::Particle2D like

  
  
  
void MSAParticleGroup2D_customparticle::draw()  
{  
    vector<TT_Custom_MSAParticle*>::iterator it = custom_group.begin();  
      
    while( it != custom_group.end() ) {  
        TT_Custom_MSAParticle * p = *it;  
        if( p->isDead() ) {  
            it = custom_group.erase(it);  
        } else {  
            ofPushStyle();  
            ofFill();  
            ofSetColor(p->getColor().r*255, p->getColor().g*255, p->getColor().b*255);  
            ofCircle( p->getPosition().x, p->getPosition().y, p->getRadius() );  
            ofPopStyle();  
            it++;  
        }  
    }  
}  
  

which is fine for one loop, but the whole MainParticleGroup class deals with a lot of more stuff where the particle group is defined as :

  
  
  
vector<Physics::Particle2D*>            group;  
  

I could use in the group classes a different group like

  
  
   vector<TT_Custom_MSAParticle*>      custom_group;  
  

but then I have to rewrite all my generic group code in each child group class using the different type. I could use the static cast in the draw loop and I would be sorted but I am wondering if there is a more OOP way to do this. For example having a function which returns the right type of group we are dealing with and use it whenever I need to access the group.

Hope that makes sense. Just want to know about people’s way to solve these type of problems

T

Hi

not sure if I understand you correctly but you have a base class and an other one that extends Base and has a custom draw function?

  
  
class Base{  
...  
void draw();  
}  
  
class Extended: public Base{  
   void draw();  
}  
  

And if you store Extended as a Base* in a vector and call the draw. Then not Extended::draw is being called but Base::draw?

If so, you should be able to change the draw function to virtual draw. It should work as expected then…

  
  
class Base{  
...  
virtual void draw();  
}  
  
class Extended: public Base{  
   void draw();  
}  
  

Yes, what underdoeg said. You shouldn’t redefine non-virtual base functions in any case. Also, if your class contains virtual functions, make sure that it has a virtual destructor, too.

hey thanks for the answers, but I think thats what I am doing already, the draw function is virtual in the base group class. The point I was trying to make is that in the child class the particle itself might be of a different kind than in the base class, in the base class the draw loop works on the generic Phyics::Particle2D class. In the child group class where I want for example draw a color of the particle the in the draw loop the particle is of the type Particle2D_colored. its easy to just change the draw loop but all other operations on the particle complain as well since the particle is suddenly of a different type.

sorry just realizing this must be really complicated to understand. Maybe I get some time and can make a pseudcode example.

Consider designing a common interface for the Particle classes you’re using. That could be a (virtual) base class MyParticle. Then you create wrappers for 3rd party Particle classes to implement that interface or implement (inherit) it directly in your own custom Particle classes.

The aim should be to use a vector everywhere instead of different custom classes. The latter will inevitably lead to a lot of code duplication, like in your draw-example: you’re re-writing everything, when all that differs is the one single line about retrieving a color. So getColor() looks like one candidate for the MyParticle interface.

Hope that makes sense…

Hey Sinasonic,

super late reply but thanks for the help, an idea like this was what I was looking for. Googled it and had some really interesting results.

TT

Glad to hear!!
I realized belatedly that I used sloppy terminology - what I meant was an abstract base class, not virtual. Anyway, I guess you found the right concept to fit your needs.