不可能:将此指针作为默认参数.为什么? [英] Not possible: this pointer as a default argument. Why?

查看:23
本文介绍了不可能:将此指针作为默认参数.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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++?

这篇关于不可能:将此指针作为默认参数.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆