2016-09-08 9 views
0

またはstd::make_uniqueを何らかの形にキャストする必要がありますか?私は私が使用できるいくつかの機能をFOOクラスを持っているstd :: make_uniqueをキャストして、クラスで宣言された関数を使用する方法は?

:今まで

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

(:別の.cppで

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

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

、私の与えられたコードは、以下の方法を使用してFOOをinitatedすべてが正しい)。私がしたいことは、私のnew_fooでexecuteを呼び出すことです。私は

new_foo.execute(); 

と試みたが、それは言う:

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

executeがメンバー<WORK::TOBEDONE::FOO>が、STDに呼ばれることができるはずです:: unique_ptrをは私が何をすべきかを理解するために苦労を与えています。

答えて

3

new_foo->execute();

unique_ptrその意味で、通常のポインタのように動作し、かつ過負荷にoperator->operator *を持っています。

あなたが指示先にアクセスするために ->*を使用している間( std::unique_ptr::getのような) unique_ptr機能にアクセスするために定期的なドット( .)を使用

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 
関連する問題