c++ - 如何在代码中读取嵌套结构?

标签 c++ oop nested

我正在尝试创建一个结构,其中具有嵌套结构(子结构)。如何定义这样的子结构,以及如何从外部访问它?

这是我的结构:


    struct Uniform
        {
           std::string color;
        };

    struct Team
    {
        std::string tname;
        int poensHome;
        int poensGuest;
        Uniform teamUniform;

    };

    struct Player
    {
        std::string name;
        std::string surname;
        int goals;
        Team* team;
    };

这是应该将值读入团队的函数,我如何读取嵌套结构Uniform UniformTeam的值?
   void read1(Team a[],int n)    
    {
        int i;
        for(i=0;i<n;i++)
        {
            cout<<endl<<"Name of the team:";
            cin>>a[i].tname;
            cout<<endl<<"Poens of the team (home):";
            cin>>a[i].poensHome;
            cout<<endl<<"Poens of the team(guest)";
            cin>>a[i].poensGuest;


        }
    }

我是否通过以下方式阅读:
    cout<<endl<<"color  of the team uniform";
    cin>>a[i].Uniform.teamUniform;

最佳答案

以与读取任何结构成员相同的方式进行读取,关于嵌套结构(除了嵌套)没有什么特别的。

cout<<endl<<"color  of the team uniform";
cin>>a[i].teamUniform.color;

关于c++ - 如何在代码中读取嵌套结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60947741/

相关文章:

html - bootstrap cols 中字体大小的不同显示

c++ - 使用和不使用 INCLUDED_HEADERNAME_H 定义 header 有什么区别

c++ - 构造函数初始化列表与在头文件中初始化

c++ - 强制 QBENCHMARK 执行多次迭代

java - 我们可以在java中使用C++类型枚举吗?

mysql - 嵌套 mysql 查询的性能损失

c - C中带指针的嵌套结构

c++ - QTreeWidgetItem 更改 - 检测 Enter/ESC

javascript - 如何有效地在对象上应用许多不同的规则并使用 JavaScript 的面向对象技术

c# - 公共(public)实例变量访问错误: "An object reference is required for the non-static field, method, or property..."