请考虑以下代码段:
#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
的成员值初始化为其他内容?
或者我必须做一些类似Test t=Test{.value1=1,.value2=2};
的事情来获得不同的init值吗?
编辑:也许我问得有点含糊。 我的问题基本上是什么语法: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.
}