我无法使用类变量作为同一类函数的默认参数
I am unable to use a class variable as a default argument for the same class's function
我很清楚第7行是无效的。但是我想使用类变量作为方法(apple)的默认参数。
class trial{
public:
int i=10 ;
void apple(int i=this.i){
cout<<i<<endl;
}
void display(){
cout<<i<<endl;
}
};
Replace
void apple(int i=this.i){
cout<<i<<endl;
}
白马王子;
void apple(int i){
cout<<i<<endl;
}
void apple(){
apple(i);
}
不能访问函数的正式参数列表中的成员变量
你不能这样设置默认值。如果你想用相同的名称显示对象的变量而不是函数的参数,使用:
void apple(int i){
// class member
cout << this.i << endl;
// function variable
cout << i << endl;
}
相关文章:
- 如何将lambda作为模板类的成员函数参数
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 如何定义在用作函数参数时工作的类模板的转换
- C++ 类对象作为函数参数
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 类中的虚拟布尔函数参数不起作用
- 当类成员参数具有相同的名称时,如何定义构造函数?
- 类成员函数参数列表是否可以依赖于模板参数?
- 如何使用类模板参数来更改参数调用和函数签名?
- 在C++单元测试上下文中,抽象基类是否应将其他抽象基类作为函数参数
- 当C++类函数参数之一是结构时,它们的语法有什么不同
- 子类作为模板模板基类的模板参数,而该类别又是子类函数参数
- 有没有办法从基类函数参数中推断出模板参数
- C++用参数重载友元类函数参数
- 为什么派生类函数参数取基类函数参数的值
- 不能调用类函数(参数化类)
- 派生类函数参数作为基类引用导致C2678
- (ostream&outs)在类函数参数C++
- 对于计算密集型函数,类函数参数是否比c++中的类变量更好?
- C++如何泛化类函数参数来处理多种类型的函数指针