【发布时间】:2020-04-09 19:38:31
【问题描述】:
struct response {
string resp[MAXSIZE];
string type[MAXSIZE];
int n;
}res;
【问题讨论】:
-
定义了一个结构体
response,然后立即创建了该结构体的一个实例,名为res
标签: c++ struct declaration definition typespec
struct response {
string resp[MAXSIZE];
string type[MAXSIZE];
int n;
}res;
【问题讨论】:
response,然后立即创建了该结构体的一个实例,名为res
标签: c++ struct declaration definition typespec
它是一个名为 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!" };
【讨论】: