有什么方法可以使变量类型与它所在的 class 类型相同并且是静态的和 const 而不是指针?

Any way to make a variable type the same type as the class it is in and being static and a const and not a pointer?

我想要一个与 class 所在类型相同的变量。我不想使用指针。我希望它是静态的并且是常量。我还没有找到一种方法来做到这一点。我也想要一个标识符。

当我使用指针时,我遇到了所谓的“内存泄漏”。

我想要的:

class A {
     std::string str;

     A(std::string str) : str(str)
     {}

    static const A b("hi");
}

怎么样

class A {
     std::string str;

     A(std::string str) : str(str)
     {}

    static const A& getA() {
        static const A b("hi");
        return b;
    }
}

这可能是一个解决方案,具体取决于您的需要。