1

Whit is the difference between 1 and 2?

1:

namespace Foo {
    int bar;
}

2:

class Foo {
public:
    static int bar;
}

If bar is a function, then the difference is that static class member can access class private members.

But sometimes we just need to store a constant for example. Then we have a choice.

Is there any difference between these two?

And what would be the reason to choose one over another.

I have searched this, and I am surprised there's no answer already on the web.

WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
  • 5
    I specifically said this is NOT a function. Yet someone voted to close use a "function" question. – WiSaGaN Jul 12 '13 at 05:46
  • Well, we have [this](http://stackoverflow.com/questions/5793334/c-static-vs-namespace-vs-singleton) and [this](http://stackoverflow.com/questions/7802218/namespaces-vs-static-classes) and multiple others. – Rapptz Jul 12 '13 at 05:47
  • Usually we use static to keep the value consistent in all it's instances... If the value is going to stay constant then I see no point using it... – NREZ Jul 12 '13 at 05:48
  • 1
    For constants, the only reason for choosing one over the other is "association" imho. If the constant is directly related to the class, it should be a static const(!) member. If it is a "global constant" leave it in namespace scope. – arne Jul 12 '13 at 05:49

3 Answers3

2

Just in terms of the object you're declaring

The namespace declaration defines an object uniquely for the whole program. If you put that in a header file, including the header from two different source files would produce a multiple definition linker error.

The class static declaration asks the linker to share one object among all source files.

As for class vs. namespace

Classes and namespaces are completely different things. A class describes how to form an object. Class templates may also be used to form metaprograms. But a class should not be used merely to group things inside a scope, such that the class is never instantiated. That is the job of a namespace.

If you want to share the object among several sources, do this:

// Foo.h
namespace Foo {

extern int bar; // declaration

}

// Foo.cpp
namespace Foo {

int bar = 2; // definition

}

If the object is a constant, the definition is unnecessary as long as you never require bar to have an address in memory.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
1

As there are good answers here, I want to add some importand difference:

If you need to declare global variables with certain types and a unique name you can go with static variables in templates classes. This is not possible with namespaces (at least not now).

template <class T>
struct my_variable_name {
  static T value;
};


my_variable_name<int>::value=7;

my_variable_name<float>::value=5.0;
Community
  • 1
  • 1
Jan Herrmann
  • 2,717
  • 17
  • 21
1

Big difference. Apart from literal difference, they are different in the following way:

  1. A class is like a model, to reflect some state or behavior, to generate objects. A namespace is just for name isolation, and cannot be used for constructing objects.
  2. A namespace can be declared as anonymous. A class cannot.
jogojapan
  • 68,383
  • 11
  • 101
  • 131
lulyon
  • 6,707
  • 7
  • 32
  • 49