In C++, should I ever declare a local variable in the corresponding header?

So for example, I create a class with two functions:

foo.h

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class foo
{
public:
foo();
void bar();
void ey();
bool m_memberBool;
bool localBool; // ??? Should I put this here?
};
</code>
<code>class foo { public: foo(); void bar(); void ey(); bool m_memberBool; bool localBool; // ??? Should I put this here? }; </code>
class foo
{
public:
    foo();
    void bar();
    void ey();
    bool m_memberBool;
    bool localBool; // ??? Should I put this here?
};

foo.cpp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#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;
}
</code>
<code>#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; } </code>
#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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class BarState
{
private:
bool localFlag;
public:
void Bar();
// or even:
void operator()();
};
class Foo
{
BarState bar;
};
</code>
<code>class BarState { private: bool localFlag; public: void Bar(); // or even: void operator()(); }; class Foo { BarState bar; }; </code>
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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

In C++, should I ever declare a local variable in the corresponding header?

So for example, I create a class with two functions:

foo.h

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class foo
{
public:
foo();
void bar();
void ey();
bool m_memberBool;
bool localBool; // ??? Should I put this here?
};
</code>
<code>class foo { public: foo(); void bar(); void ey(); bool m_memberBool; bool localBool; // ??? Should I put this here? }; </code>
class foo
{
public:
    foo();
    void bar();
    void ey();
    bool m_memberBool;
    bool localBool; // ??? Should I put this here?
};

foo.cpp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#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;
}
</code>
<code>#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; } </code>
#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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class BarState
{
private:
bool localFlag;
public:
void Bar();
// or even:
void operator()();
};
class Foo
{
BarState bar;
};
</code>
<code>class BarState { private: bool localFlag; public: void Bar(); // or even: void operator()(); }; class Foo { BarState bar; }; </code>
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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật