- 论坛徽章:
- 0
|
原帖由 Arfei 于 2006-8-27 15:40 发表
/****************
void test(Student *p)
{
p++; //这里已经改变了p的地址;
}
**********/
p入栈和出栈后其位置并没有变化,你在函数内部能做的只是修改p指 ...
#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
void display();
friend int max_score(Student *);
private:
int num;
float score;
};
void Student::display()
{
cout<<"num="<<num<<" "<<"score="<<score<<endl;
}
int max_score(Student *p)
{
Student *p1=p;
p++;
for(int i=0;i<4;i++,p++)
{
if(p->score>p1->score)
p1=p;
} //这只是找出最大的那个数;
cout<<"maxnum="<<p1->num<<endl;
p->score=p1->score;
p->num=p1->num; //我已经改了p->num的值,为什么下面输出还是没有改?
} //当我把整个函数体换作,p->num=200的时候,输出又
能改变,为什么啊?
int main()
{
Student stud[5]={
Student(101,78.5),
Student(102,85.5),
Student(103,98.5),
Student(104,100.0),
Student(105,95.5)
};
Student *p=stud;
max_score(p);
p->display();
return 0;
} |
|