If you want to have functions within a library that only the library itself should be able to use or variables that shouldn't be accessed from outside, you usually make them private by writing this in the header file:
class Lib{
public:
// All these can be accessed from outside
void function1();
void function2();
int someNumber;
private:
// These two can't be accessed from outside
void secretFunction();
int secretNumber;
};
My question is this:
I want to write this library without using classes. How do I protect things from being accessed from anywhere in this case?
1 Answer 1
If your library is implemented in a single .cpp file, you can define these
variables and functions within that file, and give them the static
qualifier:
static int my_private_variable = 42;
static void my_private_function()
{
...
}
Obviously, there should be no mention of them in the .h file.
Note also that we are talking about global variables. Local variables are already hidden within the defining function.
-
Exactly what I was looking for. Check out this question: stackoverflow.com/questions/17554515/… you basically answered that too!LukasFun– LukasFun2019年11月28日 18:11:35 +00:00Commented Nov 28, 2019 at 18:11