Home > Enterprise >  Is there an actual security risk with using public for a certain class attribute? [duplicate]
Is there an actual security risk with using public for a certain class attribute? [duplicate]

Time:09-17

I understand that public private and protected serve the programmer to not get mixed up regarding the right way to modify data. Is there some sort of security risk associated with making some data public ? For exampl, let's say i make a game that uses the following class :

Class A
{
public:
int speed;
Vector2 position;

void render();

}

If someone was playing the game (from an executable), could there be some way for them to change the speed variable ?

CodePudding user response:

public access is purely a compile-time concept/control - it doesn't make it harder or easier to modify the value at runtime in e.g. a debugger. The memory layout of an object of class A will have speed and position sitting there regardless. Compared to having the members private, the main difference is whether some client code using the object would effectively have to call some other member function to affect a modification to those data members - if those functions were inlined, then it might end up with the same compiled code as modifying the data members directly from the client code.

CodePudding user response:

If someone was playing the game (from an executable), could there be some way for them to change the speed variable ?

Access modifiers don't protect the memory in the binary. If someone knows where that value is located in memory then this person could modify it.

Access modifiers are only there in the language to tell how the members should be accessed.

  • Related