I have some functions in a namespace I made that are used throughout my program.
In header file:
namespace NQueens
{
static int heur = 0;
int CalcHeuristic(char** state, int size);
void CalcHorzH(char ** state, int &heuristic, int size);
void CalcColH(char ** state, int &heuristic, int size);
void CalcDiagH(char ** state, int &heuristic, int size);
int calcCollisions(int queensPerRow, int size);
}
Everything works fine. However the only function that actually gets called from my outside program code is the CalcHeuristic(char** state, int size)
function.
This function then calls the other functions itself.
Since these do not belong to a class my compiler will not let me declare the other functions as private
. Is there a way to do this? Should I even worry about it?
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
Explanation: The member functions can be accessed inside the class only if they are private. The access is scope is limited to ensure the security of the private members and their usage. 2.
Private and Protected Members in C++ A class in C++ has public, private and protected sections which contain the corresponding class members. The private data members cannot be accessed from outside the class. They can only be accessed by class or friend functions. All the class members are private by default.
You should make a function private when you don't need other objects or classes to access the function, when you'll be invoking it from within the class. Stick to the principle of least privilege, only allow access to variables/functions that are absolutely necessary.
A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private.
The member function should be private when the function is needed for internal processing, but not useful to the program outside the class. In some cases a class may contain member functions that initialize member variables or destroy their contents.
Don't declare them in the header, put them in an anonymous namespace in the implementation file.
Example header:
namespace NQueens
{
int CalcHeuristic(char** state, int size);
}
Example implementation:
namespace
{
static int heur = 0;
void CalcHorzH(char ** state, int &heuristic, int size);
void CalcColH(char ** state, int &heuristic, int size);
void CalcDiagH(char ** state, int &heuristic, int size);
int calcCollisions(int queensPerRow, int size);
}
namespace NQueens
{
int CalcHeuristic(char** state, int size)
{
// ...
}
}
namespace
{
void CalcHorzH(char ** state, int &heuristic, int size) {}
void CalcColH(char ** state, int &heuristic, int size) {}
void CalcDiagH(char ** state, int &heuristic, int size) {}
int calcCollisions(int queensPerRow, int size) { return 0; }
}
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