简体   繁体   English

如何使用函数初始化静态常量成员

[英]How to initialize static constant member using function

In a very simplified case, I have the following setup in which, I simply want to initialize a constant static member(of class foo) from functions of class A (singleton and instance are unrelated to this question): 在一个非常简化的情况下,我具有以下设置,在该设置中,我只想从A类的函数中初始化一个常量静态成员(foo类)(单例和实例与该问题无关):

class A
{
public:

    static A instance;
    A & getInstance() { return instance; }
    int i(){ return 10;}
    int j(){ return 20;}
};

class foo {
public:

    static const int ii = A::getInstance().i() * A::getInstance().j(); 
};
const int foo::ii;

int main()
{
    foo f;
    return 1;
}

the aim is to initialize member ii using some function as above. 目的是使用上述某些函数初始化成员ii。 but it generates the following error: 但它会产生以下错误:

$ c++ static_constant.cpp 
static_constant.cpp:14:30: error: ‘A::getInstance()’ cannot appear in a constant-expression
static_constant.cpp:14:42: error: a function call cannot appear in a constant-expression
static_constant.cpp:14:44: error: ‘.’ cannot appear in a constant-expression
static_constant.cpp:14:46: error: a function call cannot appear in a constant-expression
static_constant.cpp:14:53: error: ‘A::getInstance()’ cannot appear in a constant-expression
static_constant.cpp:14:65: error: a function call cannot appear in a constant-expression
static_constant.cpp:14:67: error: ‘.’ cannot appear in a constant-expression
static_constant.cpp:14:69: error: a function call cannot appear in a constant-expression

could you please help me out? 你能帮我吗? will Appreciate it. 会赞赏的。

You are using getInstance as a static function, but it's not declared static . 您将getInstance用作静态函数,但未将其声明为static

Change the declaration of getInstance : 更改getInstance的声明:

static A & getInstance() { return instance; }

Several issues with the code, but here's a full compilable sample: 代码有几个问题,但这是一个完整的可编译示例:

class A
{
public:

    static A instance;
    static A & getInstance() { return instance; }
    int i(){ return 10;}
    int j(){ return 20;}
};

class foo {
public:

    static const int ii;
};
const int foo::ii = A::getInstance().i() * A::getInstance().j(); 
A A::instance;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时使用静态成员函数初始化静态成员变量? - How to initialize a static member variable using a static member function at runtime? 使用静态成员函数初始化初始化列表中的常量成员变量是否有任何问题? - Are there any problems in using a Static Member Function to initialize a Constant Member Variable in an Initializer List? 如何在子类中初始化静态常量成员变量? - How do I initialize static constant member variables in a subclass? 如何使用函数的结果初始化静态成员数组? - How to initialize static member array with a result of a function? 在嵌套类中初始化静态常量成员变量 - Initialize static constant member variable in nested class 如何初始化静态成员 - How to initialize a static member 如何使用常量成员值初始化数组成员? - How to initialize the array member with constant member value? 如何使用该对象的 static 成员 function 初始化 object? - How do I initialize an object using a static member function of that object's class? C ++ —如何在成员函数内部初始化静态变量? - C++ — how to initialize the static variable inside a member function? 如何使用函数指针作为参数初始化模板的静态成员 - How to initialize the static member for template with function pointer as parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM