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{
    // All these can be accessed from outside
    void function1();
    void function2();
    int someNumber;
    // 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.