Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:17:58+00:00 2026-06-10T17:17:58+00:00

Suppose you have the following non-static member function: // .h struct C { void

  • 0

Suppose you have the following non-static member function:

// .h
struct C {
    void f();
}

Now suppose you want to implement C::f by using some sub-functions specific to C::f, to make it shorter and more readable; for example:

// .cpp
void C::f() {
    h1();
    h2();
    //...
    hn();
}

suppose that many of those h()’s functions do not need to access any data members. This means that you may well define the functions either as static free functions (in one .cpp) or as member functions (static or not).

Would you make them static free functions or function members of C?

One advantage in the first case is that you will not have to declare them in C:

// .cpp
static void h1() {//...}
static void h2() {//...}
static void hn() {//...}

Also, if I am not wrong, there is no risk of global namespace pollution, since they are static, that is they can only be seen from other functions inside the same unit .cpp (in which C::f is also defined).

while in the second case you will have to declare them in C, although, as I said, they are only supposed to be used by C::f.

// .h
struct C {
    void f();
  private:
    static void h1(); // can be either static or non-static
    static void h2();
    static void hn();
}

// .cpp
void C::h1() {//...}
void C::h2() {//...}
void C::hn() {//...}

I think the second version results in bloated code for no good reason, above all if you end up having to split other long member functions like C::f to make the code more readable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:18:00+00:00Added an answer on June 10, 2026 at 5:18 pm

    I would suggest using an anonymous namespace:

    namespace {
        void C::h1() {/...}
        void C::h2() {/...}
        void C::h3() {/...}
    }
    

    (see this question)

    That way, you guarantee that the function is not visible outside the file it is defined and thus you are in no way polluting your global namespace – which would be my main concern for free static functions.

    Making them private members of your class exposes the function interface to the whole world (as you publish your *.h file) and thus just makes the interface more complicated for no reason. (There are more arguments you could add here, for example higher compilation times when changing the definition of a private function)

    Searching for “anonymous namespaces” results some interesting discussions on this topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following simple struct: struct Vector3 { double x; double y;
Suppose I have the following directory structure in Vim's NERDTree: /some/folder/ |-apples.txt |-bananas.txt |-
Suppose you have the following string: Hello... how are you!!?? I'm bored!! I want
Suppose I have the following code: namespace x{ class X{ virtual void x(){} }
Suppose, I have a following text file, which contains: Non-Leaf Node: level=1 count=27 address=52
suppose I have following test/ Main.py test/one/ One.py test1.txt test/two/ Two.py test2.txt What I
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Suppose I have the following js code: r='d|daa|dd'; reg = /\|.*/; result = reg.exec(r);
suppose I have the following matrix a = 2 NaN NaN 4 NaN 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.