0

I am currently writing a Vector2d class in c++ and there is an "Angle" method which is used without an instance of vector2d (static). The problem is, user is able to call other non-static methods as you see here(all the other methods are non-static and therefore should not be seen).

enter image description here

But I don't want them to access the non-static methods. How can I do that?

Here is my .h and .cpp files for the Angle method.

Vector2D.h

static float Angle(Vector2D v, Vector2D v2);

Vector2D.cpp

float Vector2D::Angle(Vector2D v, Vector2D v2)
{
    float _angle = acosf(v.Dot(v2) / (v.Magnitude() * v2.Magnitude()));
    return _angle * 180 / PI;
}

Note: I use struct for Vector2D

I examined Unity's Vector2 class. They have exactly what I want to have I've tried to make the methods private but it does not solve anything

classworm
  • 3
  • 3
  • 1
    Make the method `private`? – Jason Dec 04 '22 at 17:14
  • you can't "disable" these calls unless making them private. The access to a non-static member function is valid, an example would be `std::invoke(std::bind(&Vector2D::normalize, vec));` where `vec` is a `Vector2D` object. This would call `.normalize()` on `vec`. – Stack Danny Dec 04 '22 at 17:24
  • 1
    You are asking about the members of Vector2D other than Angle, but are only showing the declaration for Angle(). We cant see if what you really have is what you think you have. Also, the message you are showing is from your IDE, not the compiler. Perhaps its an IDE tool issue. If you put in one of the non-static member functions in this context and try to compile does it generate a compiler error or not? The compiler gets the real say here. – Avi Berger Dec 04 '22 at 17:25
  • @StackDanny I made Angle method private but now I can't call it by Vector2D::Angle(). How can I call it now? – classworm Dec 04 '22 at 17:37
  • why make the static private? you wanted to hide the non-static member functions, so you can make those private. But this doesn't mean that you really should do this, it could break the whole accessibility. – Stack Danny Dec 04 '22 at 17:39
  • @StackDanny I only want to access Angle by Vector2D::Angle but when make Angle public in .h I can access that by --> Vector2D newVector; newVector.Angle() --> I don't want this. I only want access Angle by Vector2D::Angle – classworm Dec 04 '22 at 17:44
  • Your last comment is sounding different than the ? in the post. A static member function can be called with or w|o an object. A non-static only with. (Presuming the caller has proper access re public/protected/private.) AFAIK there is nothing that allows you to call a member fcn only if you don't have an object & I don't understand why this would be sensible or desired. – Avi Berger Dec 04 '22 at 17:59

0 Answers0