I am new to C++ and I am creating a game. I have a class called main in which I declare
Game * game; //globally
int main() {
game = new Game();
game->show();
}
My class game initiates my game etc. Now in other classes (player, enemy, etc.), I access variables from the game such as player health using
#include<game.h>
extern Game * game;
func::func() {
game->health->resetHealth();
}
Is this breaking encapsulation/ood paradigm? Is it bad practice? The thing is I can see any other way of doing it for a game.
Yes, extern
breaks encapsulation. The main concept of encapsulation is data hiding and binding property and behavior of an object in a single entity.
Making the variable extern
would break the law.
In some more advance OOP
language like java
, there is no extern
. And in Java, it always suggested making property/field private to restrict its access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With