Guys, I am very new to c++. I have just wrote this class:
class planet
{
public:
float angularSpeed;
float angle;
};
Here is a function trying to modify the angle of the object:
void foo(planet* p)
{
p->angle = p->angle + p->angularSpeed;
}
planet* bar = new planet();
bar->angularSpeed = 1;
bar->angle = 2;
foo(bar);
It seem that the angle in bar didn’t change at all.
Note that you are passing
barby pointer, not by reference. Pass-by-reference would look like this:Pass-by-reference has the added benefit that
pcannot be a null reference. Therefore, your function can no longer cause any null dereferencing error.Second, and that’s a detail, if your
planetcontains only public member variables, you could just as well declare itstruct(where the default accessibility ispublic).PS: As far as I can see it, your code should work; at least the parts you showed us.