我想學習C++ 17如何初始化的std ::可選的用戶定義的數據類型的
新功能,請找到下面的代碼:
class Test
{
public:
Test(int x):y(x){}
~Test(){}
int getx()
{
return x;
}
private:
int x;
};
struct Container
{
std::optional<Test> test;
};
int main()
{
struct Container obj;
// here i want to initialize the member "test" of
// struct Container
obj.test = make_optional<Test>(10); ----> is this correct??
}
可有人請讓我知道如何初始化一個std::optional
?例如,如果我聲明如下:
std::optional<Test> t
如何初始化它?
'std :: optional'將在C++ 17中。 – Zereges
@Zereges是它的C++ 17。但我想知道如何在聲明後初始化它。 – sagar
請注意,您在聲明時進行初始化。在聲明它之後,您只能分配給它。 – NathanOliver