您当前的位置: 首页 >  c++
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++ struct结构体初始化

软件工程小施同学 发布时间:2022-09-23 16:42:18 ,浏览量:0

c++ 中可以将结构体看作没有任何成员函数的对象,因此也可以使用构造函数进行初始化。

#include 
using namespace std;
struct Date
{
    int year;
    double month;
    string day;
    int a[10];
    Date()
    {
        year = 2022;
        month = 7.0;
        day = "22";
    }
    Date(int year) //这里可以使用 Date(int year=0) 来指定year默认值,但是这样就不能有Date()
    {
        this->year = year;
        month = 7.0;
        day = "22";
    }
};

int main()
{
    // 默认值初始化
    Date date1;
    cout            
关注
打赏
1665320866
查看更多评论
0.0424s