![]() |
#2
rjsp2017-05-28 22:10
|

#include <iostream>
#include <string>
using namespace std;
class Student{
public:
Student(int n,int s){
number = n;
score = s;
}
void print(){
cout<<number<<" "<<score;
}
private:
int number;
int score;
};
void cal_size(Student stu[]){
cout<<"函数内计算的大小"<<sizeof(stu)<<"---"<<sizeof(stu[0])<<endl;
}
int main(){
Student stu[5] = {
Student(1,100),
Student(2,200),
Student(3,300),
Student(4,400),
Student(5,500)
};
cal_size(stu);
cout <<"函数外计算的大小"<<sizeof(stu)<<"---"<<sizeof(stu[0])<<endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
class Student{
public:
Student(int n,int s){
number = n;
score = s;
}
void print(){
cout<<number<<" "<<score;
}
private:
int number;
int score;
};
void cal_size(Student stu[]){
cout<<"函数内计算的大小"<<sizeof(stu)<<"---"<<sizeof(stu[0])<<endl;
}
int main(){
Student stu[5] = {
Student(1,100),
Student(2,200),
Student(3,300),
Student(4,400),
Student(5,500)
};
cal_size(stu);
cout <<"函数外计算的大小"<<sizeof(stu)<<"---"<<sizeof(stu[0])<<endl;
system("pause");
return 0;
}
输出结果为:
函数外计算的大小8---8
函数外计算的大小40---8
期望结果:
函数外计算的大小40---8
函数外计算的大小40---8
问题:
为什么输出结果函数内计算大小不是40--8?是传参的问题吗?我不明白为什么会出现这样的问题。
[此贴子已经被作者于2017-5-28 20:21编辑过]