在 C++ 中,如果函数的返回值为 void,则对函数内部参数所做的更改不会反映在实际变量中,但成员函数并非如此,我们可以
看到永久发生的更改。
#include<iostream>
using namespace std;
class Student {
public:
int age;
float marks;
Student()
{
cout << "call by default";
}
void ageInc()
{
age = age + 1;
}
};
int main()
{
Student s;
s.age = 34;
cout << s.age << endl;
s.ageInc();
cout << s.age << endl;
return 0;
}