c++中struct使用注意事项 c++中struct使用注意事项

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c++中struct使用注意事项 c++中struct使用注意事项

  2021-03-20 我要评论
想了解c++中struct使用注意事项的相关内容吗,在本文为您仔细讲解c++中struct使用注意事项的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c++,struct,下面大家一起来学习吧。

1.C++的结构体变量在声明的时候可以省略struct,在c中这样是不可以的,例子如下

#include<iostream>
#include<string>
using namespace std;

struct test{
  int num;
  string name;
};

int main(void)
{
  test t;
  t.num=1;
  t.name="jack";
  cout<<t.num<<" "<<t.name<<endl;    
}

2.c++的结构体声明可以声明在main()函数中,也可以在main()函数之前,在之前的话,整个程序都可以调用,这也是最常用的方式;而在内部的话,只能在函数内使用,也就是说在声明的函数内可以使用,类似于局部变量的概念。如下

int main(void)
{
  struct test{
    int num;
  };
  
  test hello;//right
  
}

void t()
{
  test t; //wrong
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们