开发者

How to pass function reference into arguments

开发者 https://www.devze.com 2023-01-02 09:32 出处:网络
I\'m using boost::function for making function-references: typedef boost::function<void (SomeClass &handle)> Ref;

I'm using boost::function for making function-references:

typedef boost::function<void (SomeClass &handle)> Ref;
someFunc(Ref &pointer) {/*...*/}

void Foo(SomeClass &handle) {/*...*/}

What is the开发者_如何学Go best way to pass Foo into the someFunc? I tried something like:

someFunc(Ref(Foo));


In order to pass a temporary object to the function, it must take the argument either by value or by constant reference. Non-constant references to temporary objects aren't allowed. So either of the following should work:

void someFunc(const Ref&);
someFunc(Ref(Foo)); // OK, constant reference to temporary

void someFunc(Ref);
someFunc(Ref(Foo)); // OK, copy of temporary

void someFunc(Ref&);
someFunc(Ref(Foo)); // Invalid, non-constant reference to temporary
Ref ref(Foo);
someFunc(ref); // OK, non-constant reference to named object

By the way, calling the type Ref and the instance pointer when it's neither a reference nor a pointer could be a bit confusing.

0

精彩评论

暂无评论...
验证码 换一张
取 消