-1

I have a class that needs a static const variable member. The value for that class will be known at runtime by a user input. I have read a lot of post but so far none of the solution works. I am aware that this post:

Can I initialize a static const member at run-time in C++?

contains a work around, but it uses a global variable that I'd like to avoid. And also it doesn't really solve the problem.

Consider this:

#include

class A {
  friend void foo(int);
    static int dummy;
public:
    static const int &T;
};

const int &A::T = A::dummy;
int A::dummy = 0;

void foo(int val) { A::dummy = val; }

int main() {
    foo(10);
    std::cout << A::T << std::endl;
    foo(12);
    std::cout << A::T << std::endl;
}

The program will compile and write 10 and 12 in the console. Wich defeats the purpose of the const qualifier. What is const here is the reference not the value. So I have come to the conclusion that it is not possible to have a static const class variable at execution time!

I know another way involving creating a setting-type class but it is not very neat.

So if someone knows a simple solution, please let us know!

Community
  • 1
  • 1
Eric
  • 19
  • 4
  • 1
    please provide some code you tried yourself along with errors you may be getting. at this point your question is much to broad. please read http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve to ask better questions – davejal Dec 06 '15 at 00:37
  • @davejal Did you even read the question? Is it not a matter of the code I tried or the errors I got? I dont feel like my question is broad, it is pretty straight foward to me. But thanks for the down voting anyway, – Eric Dec 06 '15 at 01:19
  • 1st your question starts with best way (which leads to opinion based answers), not good way to start your question. 2nd you're asking for a simple solution (a class, tutorial or something else) without providing code you tried yourself. I'm sorry but this deserves more then 1 downvote to me. I was simply trying to help you ask a better question. – davejal Dec 06 '15 at 01:31
  • English is not my mother tongue, maybe I should have written better way instead of best way. I am not asking for a class or a tutorial type of answer. just a simplier way. In what providing code will provide a better answer? I am just trying to help you give constructive comment. – Eric Dec 06 '15 at 01:47
  • In what providing code will make it a better question I should have written! – Eric Dec 06 '15 at 01:58
  • @101010 Should be clearer now. There is a distinction that I want to point out between my post and the other post. It is easy to get confuse between the two, – Eric Dec 12 '15 at 19:26

2 Answers2

0

If you don't like the global variables you could do the following scheme:

class A {
  friend void foo(int);
    static int dummy;
public: 
    static const int &T;
};

const int &A::T = A::dummy;
int A::dummy = 0;

void foo(int val) { A::dummy = val; }

int main() {
    foo(10);
    std::cout << A::T << std::endl;
}

Live Demo

That is declare a helper static int variable as private in your class. Make a friend a function that alters the value of that helper.

If you want to initialize it only once you can change the helper function to:

void foo(int val) { 
  static int count = 0;
  if(!count) A::dummy = val;
  ++count;
}

Live Demo

Or if you don't like global functions you can make the foo function shown above a static member function of A as below:

class A {
    static int dummy;
public: 
    static const int &T;
    static void foo(int val) { A::dummy = val; }
};

const int &A::T = A::dummy;
int A::dummy = 0;

int main() {
    A::foo(10);
    std::cout << A::T << std::endl;
}

Live Demo

101010
  • 41,839
  • 11
  • 94
  • 168
  • This solution works but isn't foo a global function? So it is nearly the same solution as in the link but with a global function instead of a global variable? Btw it is not that I dont like global variable, but in the context that I am working I cant use them. – Eric Dec 06 '15 at 01:51
0

There is also something wrong with the solutions that I provided in the link of my original question and in the other solution I got. Consider this: #include

class A {
  friend void foo(int);
    static int dummy;
public:
    static const int &T;
};

const int &A::T = A::dummy;
int A::dummy = 0;

void foo(int val) { A::dummy = val; }

int main() {
    foo(10);
    std::cout << A::T << std::endl;
    foo(12);
    std::cout << A::T << std::endl;
}

The program will compile and write 10 and 12 in the console. Wich defeats the purpose of the const qualifier. What is const here is the reference not the value. So I have come to the conclusion that it is not possible the have a static const class variable at execution time!

Eric
  • 19
  • 4