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?

asked Nov 28, 2019 at 17:33

1 Answer 1

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.

answered Nov 28, 2019 at 17:55
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.