2
\$\begingroup\$

An animation is composed of frames. I have the following class:

class Frame
{
private:
    sf::IntRect rect;
    unsigned int duration;

public:
    Frame(sf::IntRect rect, unsigned int duration);

    unsigned int getDuration();
    sf::IntRect& getRect();
};

At the animation class I have the following function:

void Animation::addFrame(sf::IntRect rect, unsigned int duration) // public
{
    frames.push_back(Frame(rect, duration)); // ps: private std::vector<Frame> frames;
}

should it actually be:

void Animation::addFrame(Frame frame) // public
{
    frames.push_back(frame);
}

thus showing how animation is implemented underneath? I'm also in doubt if the frame class shouldn't simply be a struct with rect and duration. Thanks a lot in advance!

\$\endgroup\$
2
\$\begingroup\$

Never lie in your code. If you say: addFrame, I expect that passing Frame should be absolutely fine. The first function you have to provide as this class' interface is addFrame(Frame const&). Then, if you firmly believe that having the (IntRect, uint) overload is really useful when writing real client code, add this overload. But (Frame const&) should go first (I mean when you think whether or not).

BTW, I'm not sure what the Frame is, but you should consider passing it by constant reference - that's idiomatic.

\$\endgroup\$
1
\$\begingroup\$

I'd have the addFrame take a Frame&

Keeps the interface simple, and then if your frames become more complex, you won't have to make as many changes.

\$\endgroup\$
0
\$\begingroup\$

This is actually a matter of taste. for my preference, i would choose

void Animation::addFrame(sf::IntRect rect, unsigned int duration) // public
{
    frames.push_back(Frame(rect, duration)); // ps: private std::vector<Frame> frames;
}

because it hides the implementation.

Writing class is good(again this is my preference) when you have member variables(which should be kept private usually).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.