可以在 C++ 中将类声明为静态吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1635068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a class be declared static in c++?
提问by Adam Maras
Is this legal in c++ (yes i know it's legal in .net), and if so, how/why would it be used?
这在 C++ 中是否合法(是的,我知道它在 .net 中是合法的),如果是,如何/为什么要使用它?
static class foo{
public:
foo();
int doIt(int a);
};
回答by Dаn
If you're using C++/CLI, the actual syntax for static
is
如果您使用的是 C++/CLI,则其实际语法static
是
ref class Foo abstract sealed /* abstract sealed = C# static */
{
};
回答by Adam Maras
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.)
不,这在 C++ 中不受支持。static
说明符在 .NET 中所做的唯一一件事就是强制您将所有类成员设为静态;它只是一个辅助关键字。要在 C++ 中编写静态类,您需要做的就是确保类中的每个成员都标记为static
. (编辑:和一个非公共构造函数,所以你的“静态”类不能被实例化。)
回答by Phillip Ngan
The closest equivalent of a static class in C++ is a class with only static member variables. This is known as the monostatepattern. 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.
C++ 中最接近静态类的等价物是只有静态成员变量的类。这被称为单态模式。这样的类意味着该类的所有实例都将具有相同的状态。单态实例的使用语法类似于普通类(与单例类不同),实际上单态类可以转换为常规类而无需更改其任何用法。例如
// 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
}
回答by Ken Bloom
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:
static
C++ 中文件级作用域的修饰符表示标记的标识符static
仅在定义它的文件中可见。此语法在类(仅方法和变量)中不可用,但对于使用匿名命名空间的类可以获得类似的效果:
namespace{
class Foo{};
};
回答by DigitalRoss
No, static
is for objects and functions.
不,static
用于对象和函数。
回答by James Black
As is mentioned in the following thread, C++ doesn't support a static class.
正如在以下线程中提到的,C++ 不支持静态类。
If you mean a class with no public constructor and only static variables then you can read this thread.
如果你的意思是一个没有公共构造函数而只有静态变量的类,那么你可以阅读这个线程。
回答by jdh8
A class cannot be static
. For the static class in other language, declare a class with only static
members.
一个类不能是static
。对于其他语言的静态类,声明一个只有static
成员的类。
The static
before class declaration attributes the immediately constructed object, mostly useful with anonymous classes;
在static
课前声明属性紧接构造的对象,大多是匿名类有用的;
static class Foo {} foo;
Foo
, the name of the class here, is optional.
Foo
,这里的类名是可选的。