[英]Typedef struct initialization
考慮這個片段:
#include <iostream>
typedef struct Test_ {
float value1;
float value2;
} Test;
int main()
{
Test t = Test();
std::cout << t.value1 << std::endl; // Prints 0
std::cout << t.value2 << std::endl; // Prints 0
}
我實際上在這里做什么Test t = Test();
(這叫什么: Test()
)? 是否可以使用此語法將Test
的成員值初始化為其他內容?
或者我必須做類似Test t = Test{.value1 = 1, .value2 = 2};
的事情嗎? 獲得不同的初始值?
編輯:也許我在問什么時有點含糊。 我的問題基本上是這個語法是什么: Test t = Test();
你需要的是:
#include <iostream>
struct Test {
float value1;
float value2;
};
int main()
{
Test t = {1, 2};
std::cout << t.value1 << std::endl; // Prints 1.
std::cout << t.value2 << std::endl; // Prints 2.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.