c++ - WinRT - 没有静态构造函数?

标签 c++ windows-8 windows-runtime c++-cx

我目前正在用 C++ 编写一些 WinRT 自定义控件,我的编译器/智能感知告诉我不允许使用静态构造函数。

我需要设置一些静态数据,我可以使用私有(private) bool 实例标志,在我的类的第一次实例化时我可以创建静态数据等。(有效地实现相同的目的)。

但是,也许我错过了什么,因为这看起来有点啰嗦。

什么是 WinRT/c++ 中静态构造的规范替代方法

谢谢

最佳答案

您在类内部声明了静态成员,但您必须在外部定义它们:

// In header file
class Foo
{
    static int bar;
    static int bar2;

    static int init_bar3() { return 123; }
};

// In source file
int Foo::bar;

// Define and intiailize
int Foo::bar2 = 5;

// For more complicated initialization
int Foo::bar3 = Foo::init_bar3();

关于c++ - WinRT - 没有静态构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384775/

相关文章:

c++ - 从 COBOL 过渡到 C++

c++ - 宏字符串 : what does #define __T(x) x mean? 和 __T(#x)?

c++ - (简单的构造函数概念)为什么 Foo(); 没有?做任何事情?

windows-8 - 检测 Windows 8 Store App 是否有触摸屏

c# - Mvvmcross 中是否有任何错误记录框架?

c# - 如何从 C++ 后台任务(Windows 通用应用程序)调用 C# 函数?

c++ - 在循环引用的上下文中处理循环包含

xaml - 在 Windows 8 应用商店应用程序中重新使用 XAML 路径

javascript - 如何将表单的值检索到 Windows 8 Metro javascript 中的对象中?

c# - 如何关闭 Windows Phone 8.1 应用程序