C++ struct的两个注意点
作者:网络转载 发布时间:[ 2016/1/20 11:23:19 ] 推荐标签:测试开发技术 .NET
1.C++的结构体变量在声明的时候可以省略struct,在c中这样是不可以的,例子如下
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 struct test{
6 int num;
7 string name;
8 };
9
10 int main(void)
11 {
12 test t;
13 t.num=1;
14 t.name="jack";
15 cout<<t.num<<" "<<t.name<<endl;
16 }
2.c++的结构体声明可以声明在main()函数中,也可以在main()函数之前,在之前的话,整个程序都可以调用,这也是常用的方式;而在内部的话,只能在函数内使用,也是说在声明的函数内可以使用,类似于局部变量的概念。如下
1 int main(void)
2 {
3 struct test{
4 int num;
5 };
6
7 test hello;//right
8
9 }
10
11 void t()
12 {
13 test t; //wrong
14 }
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。

sales@spasvo.com