• Advertisement
Sign in to follow this  

static member functions or not? (C++)

This topic is 2899 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm making a 2D math library. I have a struct named Line. It contains various functions that return information. One of them is CornerBetweenLines. My question is, is it better to define it like this?
struct Line
{
float CornerBetweenLines(const Line &ln);
};

Line ln1, ln2;
float corner = ln1.CornerBetweenLines(ln2);

Or like this
struct Line
{
static float CornerBetweenLines(const Line &ln1, const Line &ln2);
};

Line ln1, ln2;
float corner = Line::CornerBetweenLines(ln1, ln2);

Share this post


Link to post
Share on other sites
Advertisement
Read Meyers's Effective C++.

Generally, a binary operation (and your CornerBetweenLines function is a binary operation) should be a namespace-level function and not a member function to avoid awkward situations involving automatic type conversions and to end up with a more "natural" syntax when invoking that function.

That means your second option is generally preferred, except that there is no reason to make it a static member function. Implement it in terms of other member functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement