I have a class(let’s call it foo) which contains 20 members. The class has a method
public foo Rotate(double angle).
This method rotates calling object with certain angle and creates a new foo object. 10 members of the new object is always the same as the calling object.
What is the best way to implement this class so that I don’t have to recalculate all the 20 members every time the Rotate method is called?
Assuming that you can change your class properties outside of the class, I think your best solution would be a copy constructor.
What this essentially means is that you define a constructor in your class that takes an object of the same class as a parameter, then copies all of the object’s properties into a new object.
Since all you’re doing is assigning values, there’s no problem with efficiency (assuming the values are primitives).
Then, once you’ve initialized the new object, set the 10 different properties to their rotated counterparts.