In a C++ class, should I be placing my private functions and variables in the private section of the class header definition, or in the class source file and why?
For example:
// Header
class MyClass {
public:
void doSomething();
private:
int a = 0;
}
// Source
void MyClass::doSomething()
{
// Do something with `a`
}
or
// Header
class MyClass {
public:
void doSomething();
}
// Source
int a = 0;
void MyClass::doSomething()
{
// Do something with `a`
}
I've always thought, when programming it's best to make the scope of a function/variable as small as possible. So shouldn't restricting the scope of the var a
to the scope of the source file be best?