Can const member variable of class be initialized in a method instead of constructor?

c++, class, constants, design-patterns, initialization

Solution

A small example:

class A
{
public:
  A():a(initial_value_of_a()) {}

private:
  const int a;
  int initial_value_of_a() { return 5; /* some computation here */ };
};

Problem

I have a class and want to create a const int variable but the value for the variable is not available to me in constructor of the class. In initialization method of the class i get the value. Can I assign it in that method? As I am assigning it only once (as const says) why it isn't working? Code is as Following [Just a ProtoType] : File : A.h ``` Class A { private : const long int iConstValue; public : A(); initClassA(); } ``` File : A.cpp ``` A::A() { //CanNot initialize iConstValue (Don't have it) } A::initClassA(some params) { // calculation according to params for iConstValue iConstValue = (some value) } ``` This is not working. Somebody has any Solutions? NOTE : I Can not get value for iconstValue in constructor by any way as there are some restriction. So Please don't suggest to do that.

Original source