rombas
rombas

Reputation: 71

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!

Upvotes: 1

Views: 60

Answers (1)

rombas
rombas

Reputation: 71

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

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

Upvotes: 1

Related Questions