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

前端 未结 5 1086
南方客
南方客 2021-02-10 07:56

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

5条回答
  •  梦谈多话
    2021-02-10 08:06

    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 */ };
    };
    

提交回复
热议问题