Nonstatic member as a default argument of a nonstatic member function

后端 未结 9 1060
攒了一身酷
攒了一身酷 2020-11-28 05:56
struct X
{
   X():mem(42){}
   void f(int param = mem) //ERROR
   {
      //do something
   }
private: 
   int mem;
};

Can anyone give me just one

9条回答
  •  無奈伤痛
    2020-11-28 06:39

    As all the other answers just discuss the problem, I thought I would post a solution.

    As used in other languages without default arguments (Eg C# pre 4.0)

    Simply use overloading to provide the same result:

    struct X
    {
       X():mem(42){}
       void f(int param)
       {
          //do something
       }
       void f()
       {
          f(mem);
       }
    private: 
       int mem;
    };
    

提交回复
热议问题