2

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?

  • 6
    In confused as to why you think you might want to do this. Do you understand the difference between a member and a local variable? – Philip Kendall Apr 25 '15 at 12:16
  • 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? – user2690449 Apr 25 '15 at 12:28
  • 1
    The localBool that you have in foo::bar is different from the localBool that you currently have declared as a member in your class (foo.h). – Brandin Apr 25 '15 at 14:43
  • 1
    @user2690449 Local variables do not retain their state between method invocations (unless you make them static). Member variables do, on the other hand. So you should ask whether the thing you are naming is a state that needs to be remembered, or whether it's just a temporary object you're making for use in the method. – Brandin Apr 25 '15 at 14:46
  • localBool in the header file is an instance variable. You may think it is a local variable, but it isn't. I predict bugs caused by you confusing yourself and sometimes using the local variable, sometimes the instance variable. – gnasher729 Apr 25 '15 at 20:03
15

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.

1

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 to Bar
0

Member variablesare 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:

  1. Member variable is an attribute of class. which means it might be used by methods and/or manipulators of the class objects.
  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.