爱讨论这个话题的,有两种人呢,一种是牛逼的人,一种是装逼的人,不说了。
某牛人说过, C语言中的全局变量和函数,就类似于C++中的成员变量和方法, 我肤浅地认为这句话相当有道理。不多说,看两个程序吧:
- #include <iostream>
- using namespace std;
-
-
- int weight = 100;
-
- void print(int w)
- {
- cout << w << endl;
- }
-
- int main()
- {
- print(weight);
-
- return 0;
- }
- #include <iostream>
- using namespace std;
-
- class A
- {
- private:
- int weight;
- public:
- A(int w = 100)
- {
- weight = w;
- }
-
- void print()
- {
- cout << weight << endl;
- }
- };
-
- int main()
- {
- A a;
- a.print();
-
- return 0;
- }