不可能:此指针作为默认参数.为什么?

以下代码无法编译.为什么?

The following code won't compile. Why?

class A
{
   int j;
   void f( int i = this->j );
}

编辑,为清楚起见.这就是我试图做的,使用更少的代码行......

Edit, for clarity. This is what I was trying to do, using less lines of code...

class A
{
   void f( int i ){};
   void f( );
   int j;
};

void A::f()
{
    f( j );
}

推荐答案

默认参数值在编译时绑定.

Default argument values are bound at compile time.

this"只在运行时定义,所以不能使用.

"this" is only defined at run time, so can't be used.

查看这里以获得更完整的解释:C++中的默认函数参数必须是常量吗?

See here for a fuller explanation: Must default function parameters be constant in C++?

相关文章