[英]C++ Passing an array of struct as function argument
C ++:如下所示的代碼與在功能聲明和定義中同時省略關鍵字struct
的代碼之間有什么區別-代碼似乎在兩種方式上均相同?
#include <iostream>
#include <string>
struct student{
int age;
int number;
};
void printme( struct student m[]); // if 'struct' is omitted the code works as fine
int main()
{
student s[3];
s[0].age = 10;
s[0].number = 333;
printme(s);
return 0;
}
void printme( struct student m[]){
printf("George age and number: %d, %d \n", m[0].age, m[0].number);
}
這來自C,您必須在其中指定struct(或使用typedef)。 在C ++中, 只要您不對結構和對象使用相同的ID,就沒有區別。 請,不這樣做,但如果你真的想,那么你就需要編寫如struct student
的類型和student
為對象(這不一定類型的學生,如果你真的想迷惑大家) 。 基本上,C +編碼標准傾向於建議跳過struct,並且對於struct和object永遠不要使用相同的id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.