2

I was just wondering whether we can have static classes in C++. What I mean is can we declare a class as static in C++ like static class foo? I know we can have static member variables and static member functions in C++ but I am not sure about static classes.

Edit:

I intended to ask what does it mean for a class to be static.

TheRookierLearner
  • 3,643
  • 8
  • 35
  • 53
  • 2
    What would it mean, in your view, for a class to be static? – Benjamin Lindley Jan 30 '13 at 06:38
  • Are you looking for a singleton? – 1615903 Jan 30 '13 at 06:40
  • Is something like a [namespace](http://en.wikipedia.org/wiki/Namespace_%28computer_science%29#C.2B.2B) what you're looking for? – Benjamin Lindley Jan 30 '13 at 06:51
  • 1
    Damn close to this question: http://stackoverflow.com/questions/12436511/static-class-real-life-significance – jogojapan Jan 30 '13 at 07:04
  • Its actually my bad for the question being unclear (I guess that's why its down-voted). I was actually looking for what does it mean for a class to be static. I know that static variables can only be instantiated once. The same goes for static methods and they do not belong to a particular instance of a class. I'll try yo be more clear and specific next time. – TheRookierLearner Jan 30 '13 at 07:07

4 Answers4

9

static is a storage class specifier.

  • Applied to variables, it specifies the object's lifetime and visibility -- in this case, the lifetime is the entire program's execution, and the visibility is restricted to the particular translation unit (usually a given source file).
  • Applied to functions, it similarly specifies the object's visbility -- limited to the particular translation unit in which it is defined.
  • Applied to class members variables and functions, it defines the variable to be a property of the class, and not the object itself.

So that's the semi-pedantic definition. The question is, what semantics exactly would you like to attach to the idea of a "static class"? Nested classes automatically have static-like properties -- they are a property of the class, and not the individual object. If you wanted static-like properties for a class declared in an outer scope (i.e. not conflicting with the one-definition rule across different translation units), you can use an anonymous namespace.

Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66
sheu
  • 5,653
  • 17
  • 32
  • 1
    i can see two things wrong with that statement. first, a function is not "storage", but it can be static. second, some things take up space without being storage. – thang Jan 30 '13 at 06:46
  • 1
    this begs the question... who are the 5 people who upvoted that statement? – thang Jan 30 '13 at 06:49
  • @thang: eh, fine. Edited for completeness. – sheu Jan 30 '13 at 07:03
7

The static keyword implies that the object that it refers to exists through the life of the entire program. A class definition is just an outline for constructing an object.

With that in mind, perhaps you might be looking to do something like create a namespace or create a singleton object, a class that is designed to only ever have a single instance.

vmrob
  • 2,966
  • 29
  • 40
2

No, but you can basically achieve nearly the same if you create a class with static methods (and data) only. Beware thought, there is no static constructor concept in C++.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
1

If by "static class" you are referring to the ones in C#, then the equivalent in C++ is to just make a single constructor and make it private, and avoid making non-static members.

If by "static class" you are referring to the ones in Java, then all C++ classes are "static", so you can't add "static" because it would be redundant.

user541686
  • 205,094
  • 128
  • 528
  • 886