0

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

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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