#include<iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
class A{
public:
A()
{
s = { "張三",30,80 };
}
student Get(){
return s;
}
void Print()
{
std::cout << s.age << " " << &s << " From Internal" << std::endl;
}
private:
student s;
};
void printStudents(student s)
{
// 做爲參數傳遞時候時拷貝了一個新的, 原來的沒有改變
s.age = 20;
std::cout << s.age << " " << &s << " From Param" << std::endl;
}
int main()
{
A a;
a.Print();
printStudents(a.Get());
a.Print();
// 作爲參數返回後拷貝了一個新的, 原來的沒有改變
student s = a.Get();
s.age = 66;
a.Print();
std::cout << s.age << " " << &s << " From Return" << std::endl;
system("pause");
return 0;
}
輸出log
30 0034FAA4 From Internal
20 0034FA4C From Param
30 0034FAA4 From Internal
30 0034FAA4 From Internal
66 0034FA80 From Return
請按任意鍵繼續. . .