C++/CLI,类声明之外的静态构造函数
如何将托管类的静态构造函数的主体放在类声明之外?这种语法似乎是可编译的,但它真的意味着静态构造函数,还是只是一个静态(=在翻译单元之外不可见)函数?
ref class Foo {
static Foo();
}
static Foo::Foo() {}
How do I put body of static constructor of a managed class outside class declaration? This syntax seems to be compilable, but does it really mean static constructor, or just a static (=not visible outside translation unit) function?
ref class Foo {
static Foo();
}
static Foo::Foo() {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是创建 C++/CLI 静态构造函数的正确语法。您可以知道它没有创建静态函数,因为这不是有效的函数声明语法。函数必须指定返回类型。此外,如果编译器没有将 Foo() 类链接到您在类定义中声明的构造函数,编译器会抱怨 Foo() 不是类 Foo 的成员。
您可以相当容易地测试:
这将输出:
Yes, that is the correct syntax to create a C++/CLI static constructor. You can know its not creating a static function since that is not a valid function declaration syntax. Functions must have the return type specified. Also, the compiler would complain that
Foo()
is not a member of class Foo if it weren't linking it to the constructor you declared in the class definition.You can test the fairly easily:
This would output: