简体   繁体   中英

How to call a function from a pointer to that function?

I'm struggling with maybe a very simple hurdle but can't seem to understand how that works.

In my C++ project I want to provide lazy loaded classes. For this I thought of instantiating some fields of the type

std::shared_ptr<std::function<std::shared_ptr<MyClass>>> myClassLazyLoader;

At this point I'm not even sure anymore that is a valid approach, so please correct me if I'm completely off track.

I instantiate the field like this:

myClassLazyLoader = [] { return std::make_shared<MyClass>(); }

To invoke the function, I tried

myClassLazyLoader()

after which the compiler told me, as expected, that std::shared_ptr<std_function<... does not provide a call operator.

Naturally, I tried

(*myClassLazyLoader)()

but then it told me that std::function<... also does not provide a call operator which doesn't make any sense to me.

What am I doing wrong?

Thanks much in advance!

The moment I posted this I figured it out. The type should be

std::shared_ptr<std::function<std::shared_ptr<MyClass>()>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM