10

Is this legal in c++ (yes i know it's legal in .net), and if so, how/why would it be used?

static class foo{ public: foo(); int doIt(int a); }; 
1
  • 4
    "yes i know it's legal in .net" Not true. In C++/CLI static classes are declared as ref class MyClass abstract sealed, not static class MyClass (It's the C# syntax). Commented Oct 28, 2009 at 13:27

7 Answers 7

30
+50

If you're using C++/CLI, the actual syntax for static is

ref class Foo abstract sealed /* abstract sealed = C# static */ { }; 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 This is the correct answer. By marking a class as such, attempting to instantiate the class in C# with var f = Foo(); will give the expected errors "Cannot declare a variable of static type Foo" and "Cannot create an instance of the static class Foo", indicating that C# considers it a (C#) "static" class.
22

No, this isn't supported in C++. The only thing the static specifier does in .NET is force you to make all the class members static; it's just a helper keyword. To write a static class in C++, all you need to do is make sure each member in the class is marked as static. (Edit: and a non-public constructor, so your "static" class can't be instantiated.)

9 Comments

You also need a non-public (ideally private) constructor.
Ah, yes. I knew I was forgetting something!
Normally, this would be in a namespace.
There are situations when you make it a class. To use as a policy in a template for example.
This answer is wrong. C# static keyword adds metadata that make a class abstract and sealed so even if you reference an assembly with that class you can't instantiate the class or derive from it.
|
7

The static modifier at file-level scope in C++ indicates that the identifier marked static is only visible in the file in which it is defined. This syntax is not available in on classes (only methods and variables), but a similar effect can be obtained for classes using an anonymous namespace:

namespace{ class Foo{}; }; 

Comments

7

The closest equivalent of a static class in C++ is a class with only static member variables. This is known as the monostate pattern. Such a class means that all instances of this class will have the same state. The usage syntax of a monostate instance is similar to a normal class (unlike a singleton class), and indeed a monostate class can be converted to a regular class without changing any of its usages. E.g.

// Monostate class public class Administrator { private: static int _userId; public: int UserId() { return _userId; } } // Initializing the monostate value int Administrator::_userId = 42; // Using an instance of a monostate class void Foo() { Administrator admin = new Administrator(); Assert.Equals( 42, admin.UserId() ); // will always be 42 } 

Comments

1

No, static is for objects and functions.

Comments

0

As is mentioned in the following thread, C++ doesn't support a static class.

If you mean a class with no public constructor and only static variables then you can read this thread.

http://www.daniweb.com/forums/thread122285.html#

Comments

0

A class cannot be static. For the static class in other language, declare a class with only static members.

The static before class declaration attributes the immediately constructed object, mostly useful with anonymous classes;

static class Foo {} foo; 

Foo, the name of the class here, is optional.

Comments