2016-09-08 31 views
0

或者我需要將std::make_unique轉換成任何形式?如何投射std :: make_unique,以便我可以使用類中聲明的函數?

我有一些功能的Foo類,我可以使用:

FOO::FOO(const yoo &yoo, float numbers) : 
    num_to_execute(numbers) 
{ 
... 
... 
} 

void FOO::execute() 
{ 
    execute(num_to_execute); 
} 

在另一個的.cpp,我給出的代碼initated FOO使用下面的方法:

new_foo = std::make_unique<FOO>(yoo, number); 

(到現在爲止一切都是正確的)。我想要做的是在我的new_foo上調用execute。我試着用

new_foo.execute(); 

但它說:

error: 'class std::unique_ptr<WORK::TOBEDONE::FOO>' has no member named 'EXECUTE' 

execute應該能夠呼籲各<WORK::TOBEDONE::FOO>但標準::的unique_ptr是給我很難明白我應該做的。

回答

3

new_foo->execute();

unique_ptr行爲就像在這個意義上常規的指針,並具有operator->operator *超載。

您使用規則的點(.)訪問unique_ptr函數(如std::unique_ptr::get),而使用->*訪問指針對象。

auto str = std::make_unique<std::string>("hello world"); 
auto i = std::make_unique<int>(5); 

str->size(); 
*i = 4; 
str.reset(); //deletes the pointee and make str point to null 
i.reset(); //as above 
相關問題