What's wrong with this function? (accessing Vehicle::mRigid)
by Daniel Buckmaster · in Torque Game Engine · 07/25/2008 (4:55 am) · 2 replies
I'm trying to allow public acces to Vehicle::mRigid, but I don't just want to make mRigid public. I wrote a public method to get a pointer to the vehicle's Rigid. My definitions look like this:
I use the function as follows:
The problem is that cr is not being changed by getRigid - i.e., it's left as NULL. Then when I call function with cr, I'm trying to work with a null parameter (the function is supposed to take a Rigid*).
I've read up on pointers and references in C++, and to my knowledge, I'm doing it right.
class Vehicle : public ShapeBase
{
protected:
Rigid mRigid;
public:
void getRigid(Rigid* r) { r = &mRigid; }
}I use the function as follows:
Rigid* cr = NULL; object->getRigid(cr); function(cr);
The problem is that cr is not being changed by getRigid - i.e., it's left as NULL. Then when I call function with cr, I'm trying to work with a null parameter (the function is supposed to take a Rigid*).
I've read up on pointers and references in C++, and to my knowledge, I'm doing it right.
About the author
Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!
Torque 3D Owner Dan Keller
Default Studio Name
Rigid* getRigid() {return &mRigid;};What you're doing passes a pointer by value, so when r is modified the the function, it's actually a local copy. If you want to modify something in a function, you have to pass in a pointer to it. If you want to modify a pointer, you have to pass a pointer to the pointer (Rigid**). The easiest way to get around this mess is to just return the pointer, like above. Then use