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

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

问题描述

以下code将无法编译。为什么呢?

  A级
{
   诠释J;
   无效F(INT I =这个 - > j)条;
}

编辑,为清楚起见。这就是我要怎样做,用code以下行...

  A级
{
   无效F(int i)以{};
   空隙f()的;
   诠释J;
};无效A :: F()
{
    F(j)条;
}


解决方案

默认参数的值是在编译时绑定。

这是仅在运行时定义,因此不能使用。

在这里看到一个更全面的解释:
<一href=\"http://stackoverflow.com/questions/334882/must-default-function-parameters-be-constant-in-c\">Must默认参数,在C ++不变?

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" is only defined at run time, so can't be used.

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

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

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