1

I have a custom shape class, simplified for the question here, and looks like the following:

class Shape : public SpriteProperties{

};

where class SpriteProperties is:

class SpriteProperties{
Point rotationPivotPoint;
Point rotationPivtoAxes;
float width, height, xPos, yPos, zPos;
Point color;
};

In the user's code, when the user initializes the shape, he uses a function of the following prototype. The params in the function are the required properties for any shape to be represented.

void init(float _x, float _y, float _w, float _h, Point* _color)
{
    //Initialize the shape
}

However, my shapes also have additional properties such as rotation pivot points and axes, that the user should specify during this initialization process somehow, and these rotation properties might/might not come to use at a later point for the user depending on how he used the class.

void prepareRotationParams(Point* _rotationPivot, Point* _rotationAxes)
{
    //
}

My question: I am thinking of restricting the user to use this prepare rotation function which might be utilized at a later point in the code, and at the same time I do not want to give the prepare rotation params in the init function itself, since these do not fall in line with the core properties that must be explicitly specified by the user for any shape. What would be the ideal approach?

4

2 回答 2

1

Use default arguments:

void init(float _x, float _y, float _w, float _h, Point* _color, Point* rPivot=0, Point *rAxes=0)
{
    //Initialize the shape
    if (rPivot && rAxes) {
        prepareRotationParams(rPivot, rAxes);
    }
}
于 2012-12-13T06:46:09.093 回答
0

First, consider to pass in arguments in the constructor. Of course this sometimes is not possible.

Second, are the rotation parameters constant once set, or can they be set multiple times? If they only can be set once and setting them twice is not supported, reconsider your design, because you essentially have 3 initialization functions (constructor, init, prepareRotationParams), which makes it easy for the user to accidentally use a partially initialized object.

Instead of having 3 initialization functions, consider the builder pattern. http://en.wikipedia.org/wiki/Builder_pattern

于 2012-12-13T09:37:06.247 回答