Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:59:06+00:00 2026-06-04T17:59:06+00:00

In a very simplified case, I have the following setup in which, I simply

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:59:08+00:00Added an answer on June 4, 2026 at 5:59 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example (and this is very simplified), suppose I have a class for every
I have a very simplified question regarding sequenced indices in boost multi index. The
This is my situation, very much simplified. My classes; public class ClassBase { [Key]
I have a pure ActionScript 3 problem, but the simplified test case I've prepared
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I have a very high-traffic table with a char(50) field which takes part in
I have the following code to do regex pattern-matching. class UserAgent(val ua: String) {
I have a following (simplified) code: var myModule = { submitDummyForm: function(){ console.log(this); //
I have a document like this (this is very simplified): { 'name' : 'test',
I have a table with the following (simplified) structure: INT id, INT type, INT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.