Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can non-class functions be made private?

Tags:

c++

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?

like image 618
BradStell Avatar asked Oct 05 '15 16:10

BradStell


People also ask

Can a class function be private?

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.

Can member functions be made private?

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.

Can member functions be private in C++?

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.

Should functions be public or private?

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.

Are class member functions private by default?

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.

Why would a member function be declared 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.


1 Answers

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; }
}
like image 117
molbdnilo Avatar answered Oct 11 '22 00:10

molbdnilo