I was wondering if it is possible to create class methods that are only available statically.
Given a simple Vector3 class:
class Vector3
{
public:
float X;
float Y;
float Z;
Vector3(): X(0), Y(0), Z(0) {}
Vector3(float x, float y, float z): X(x), Y(y), Z(z) {}
static Vector3 CrossProduct(const Vector3& rhs, const Vector3& lhs);
};
Vector3 Vector3::CrossProduct(const Vector3& rhs, const Vector3& lhs)
{
Vector3 crossProductVec;
crossProductVec.X = rhs.Y * lhs.Z - rhs.Z * lhs.Y;
crossProductVec.Y = rhs.Z * lhs.X - rhs.X * lhs.Z;
crossProductVec.Z = rhs.X * lhs.Y - rhs.Y * lhs.X;
return crossProductVec;
}
and I can use it like this:
Vector3 vec1(0,1,0);
Vector3 vec2(1,0,0);
Vector3::CrossProduct(vec1, vec2); //this should work, and does.
//Static CrossProduct method.
vec1.CrossProduct(vec1, vec2); //this shouldn't work, but it does.
//I don't want the CrossProduct
//instance method to be available
I would like CrossProduct
to only be available statically. Is this possible?
I understand that the code above will not achieve what I want, I wish to know what changes could be made to achieve what I want.
Edit:
The CrossProduct() does not necessarily have to be part of the class, but I am hoping for it to appear as Vector3::CrossProduct()
. I'm open to all suggestions to achieve the required result. After that a decision of whether it's a good idea or not can be worked out.