函式參數為物件
以下為傳參考與傳指標的方式。
但是類別本身是值,不是記憶體位址,所以傳入的參數為指標,要加上&物件。
若傳入的參數為陣列,陣列名預設為記憶體位址,所以傳入函式「不用」加&陣列名。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Student {
public:
const char* name_;
int id_;
};
void callRef(const Student& s) {
cout << "ref = " << s.name_ << endl;
}
void callPoint(Student* s) {
cout << "point = " << s->name_ << endl;
}
int main() {
Student s;
s.name_ = "Bill";
callRef(s);
callPoint(&s);
return 0;
}
ref = Bill
point = Bill