【问题标题】:What is purpose of res; in following struct?资源的目的是什么;在以下结构中?
【发布时间】:2020-04-09 19:38:31
【问题描述】:
struct response {
    string resp[MAXSIZE];
    string type[MAXSIZE];
    int n;
}res;

【问题讨论】:

  • 定义了一个结构体response,然后立即创建了该结构体的一个实例,名为res

标签: c++ struct declaration definition typespec


【解决方案1】:

它是一个名为 res 类型为 struct response 的对象的声明。

结构定义可以用作类型说明符,类似

int res;

但你可以放置结构定义而不是 int 类型

这是一个演示程序

#include <iostream>

int main() 
{
    struct Hello
    {
        const char *hello;
        const char *world;
    } hello = { "Hello", "World!" };

    std::cout << hello.hello << ' ' << hello.world << '\n';

    return 0;
}

它的输出是

Hello World!

你可以在一行中写出对象hello的声明

struct Hello {  const char *hello; const char *world; } hello = { "Hello", "World!" };

但这不太可读。

其实和写是一样的

    struct Hello
    {
        const char *hello;
        const char *world;
    }; 

    Hello hello = { "Hello", "World!" };
    // or 
    // struct Hello hello = { "Hello", "World!" };

【讨论】:

  • 谢谢。这更有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2022-01-01
  • 2010-11-05
相关资源
最近更新 更多