Sign in to follow this  
sheep19

static member functions or not? (C++)

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this