【问题标题】:c++ initialization of struct with std::list included [duplicate]包含std :: list的结构的c ++初始化[重复]
【发布时间】:2018-10-16 15:05:50
【问题描述】:

我有以下问题。

检查下面的代码块,它正确地初始化了我的结构中的成员。

typedef struct
{
    int var00;
    int var01;
}struct_;

int main()
{


    struct_ my_struct;
    memset(&my_struct,'\0',sizeof(struct_));
    return 0;
}

我现在的新结构(见下文)还包括一个 std::list。我现在要做的, 在代码中保留 memset 命令?

typedef struct
{
    int var00;
    int var01;
    std::list<int> my_list
}struct_list_included;

int main()
{


    struct_list_included my_struct;
    memset(&my_struct,'\0',sizeof(struct_list_included));
    return 0;
}

【问题讨论】:

  • 为什么不简单地提供一个构造函数呢?此外,C++ 中不需要这些 typedef。
  • 如果你有 C 背景并且正在学习 C++,我建议你给自己一个 good C++ book。你的代码中有很多 C-ism 是不必要的,因为 C++ 已经内置了机制来做这些事情。

标签: c++ struct


【解决方案1】:

std::list 不是 POD 类型,所以我认为你不能通过memset() 做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2016-08-26
    • 2019-10-05
    相关资源
    最近更新 更多