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):
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. 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.
Several issues with the code, but here’s a full compilable sample: