-5

我很清楚第 7 行是无效的。但我想使用类变量作为 method(apple) 的默认参数。

class trial{

public:

int i=10 ;

    void apple(int i=this.i){
       cout<<i<<endl;
    }

    void display(){
         cout<<i<<endl;
    }
};
4

2 回答 2

2

代替

void apple(int i=this.i){
     cout<<i<<endl;
}

… 和

void apple(int i){
     cout<<i<<endl;
}

void apple(){
     apple(i);
}

您不能访问函数形式参数列表中的成员变量。

于 2016-09-27T09:38:21.337 回答
0

您不能像这样设置默认值。如果要显示对象的变量 insted 函数的同名参数,请使用:

void apple(int i){
     // class member
     cout << this.i << endl;
     // function variable
     cout << i << endl;
}
于 2016-09-27T09:48:18.440 回答