-1

Does static mean anything on func2 in the context of a namespace? Both methods appear to be equivalent.

// MyHeader.h
namespace TestNameSpace
{
    int func1() { return 1; }
    static int func2() { return 2; }
}

// SomeFile.cpp
#include "MyHeader.h"
// ...
int test1 = TestNameSpace::func1(); // 1
int test2 = TestNameSpace::func2(); // 2
AlainD
  • 5,413
  • 6
  • 45
  • 99
  • 2
    what effect does it have outside of a namespace? same effect... – user253751 Aug 03 '22 at 17:04
  • In a header file, a `static` function with an implementation is very odd. In a `cpp` file, `static` hides the function from the linker and therefore from anyone outside the file. – Silvio Mayolo Aug 03 '22 at 17:07
  • @user253751 Feel like that's misleading. "Outside of a namespace" could include "Inside the definition of a class", in which case the effect is actually quite different. – Nathan Pierson Aug 03 '22 at 17:16

1 Answers1

6

static functions (which are not member of classes) are only visible in the compilation unit they are defined in. Apart from that there should not be any difference between those two

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • 2
    Minor caution: this is correct for static functions that are not members of classes. A static member function is completely different. +1. – Pete Becker Aug 03 '22 at 17:24