I've got some embedded C++ code that's currently written in a very C-like way, and I'd like to convert it to use namespaces for better code organization. Currently I hide my private file scope functions and variables in anonymous namespaces, but I'm not sure where to hide it using this new pattern. Should I still use an anonymous namespace, or is adding it to the namespace in the .cpp file, but not the header file sufficient enough to prevent external access?
More specifically, I've got code that looks like this:
UI.h
#ifndef UI_H
#define UI_H
//Public data declarations
extern int g_UiPublicVar;
//Public function declarations
void UI_PublicFunc();
#endif
UI.cpp
#include "UI.h"
//Private data and functions
namespace
{
int m_PrivateVar = 10;
void privateFunc()
{
//Do stuff!
}
}
//Public data definitions
int g_UiPublicVar = 10;
//Public function definitions
void UI_PublicFunc()
{
m_PrivateVar++;
privateFunc();
}
...and I'd like to restructure it to look like this:
New UI.h
#ifndef UI_H
#define UI_H
namespace UI
{
//Public data declarations
extern int publicVar;
//Public function declarations
void publicFunc();
}
#endif
New UI.cpp
#include "UI.h"
namespace UI
{
//Public data definitions
int publicVar = 10;
//Public function definitions
void publicFunc()
{
m_PrivateVar++;
privateFunc();
}
}
...where should I put m_PrivateVar and privateFunc()?