So for example, I create a class with two functions:
foo.h
class foo
{
public:
foo();
void bar();
void ey();
bool m_memberBool;
bool localBool; // ??? Should I put this here?
};
foo.cpp
#include "foo.h"
foo::foo() {
bool m_memberBool = true;
}
void foo::bar() {
bool localBool = true;
if (localBool && m_memberBool) {
ey();
}
}
void foo::ey() {
m_memberBool = false;
}
Should I ever include that local variable, or any local variable ever in a header? If not, should I ever document it in the header?
5
Technically, the correct answer to your question is that it’s a contradiction in terms. As soon as you raise a variable into the class definition, it is no longer a local variable, it is a member variable.
But I assume you’re asking whether or not there’s ever a good reason to raise a local variable up into member variable status. This question comes down to the purpose of your class and that variable.
The general rule of thumb is: all variables should be declared in the narrowest possible scope. In other words, do not declare it where all of foo can use it if only foo::bar needs to use it. It’s generally a bad idea for code to have access to variables it doesn’t actually need, because that increases the risk that they will eventually depend or rely on those variables having certain values at certain times, which couples them to all the other code using those variables, making it harder to change one part without breaking others.
Do the other methods of foo actually need to access this bool? If not, keep it local. You can always raise it later if a good reason presents itself.
Should I ever include that local variable, or any local variable ever in a header?
The only case I can think of is if you write an inline or a template function: For those, the function/method body should be in the header. Then, obviously, local variables are in the header, too.
If not, should I ever document it in the header?
There might be rare instances when a caller must know about a local variable of a function. E.g. if stack space is scarce and the function uses a 100k local array variable. Or if it’s a template function that non-obviously creates an instance of its template parameter – that information might be important to the caller.
But those are extreme and construed edge cases. In the general case, the header should only document the external “contract” your functions offer, behavior that the caller can rely on even in future versions of the code.
I guess another way to ask this, is if a variable that I am using is only used in one function; should I always just make it a local variable, or are there cases where that variable should be made a member?
As @Brandin said: You would make it a member, if you the need to preserve state between different calls.
However, it might be a cleaner design to split that member and function off into a separate class, and have a member of that class instead:
class BarState
{
private:
bool localFlag;
public:
void Bar();
// or even:
void operator()();
};
class Foo
{
BarState bar;
};
That way it’s clear that:
- the “function”
Bar
retains state between calls - the member
localFlag
is meant to be private toBar
Member variables
are the value types that is associated with the class and thus will be part of Object of that class.
Local variables
are the value holders that are local to scope. Scope here could be functions
, if conditions
or swith case
, typically everything where you use {}
.
Now if you have to decide where to put which then use these rules of thumb:
Member variable
is an attribute of class. which means it might be used by methods and/or manipulators of the class objects.Local variable
is required locally for manipulations or as temporary value holder, which means it is useful for current scope only.
I hope that helped. Please comment queries if any.