views:

61

answers:

1

Hi, 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 best way to pass Foo into the someFunc? I tried something like:

someFunc(Ref(Foo));
+4  A: 

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.

Mike Seymour
Ok, that's work. Thank you for such help during this day =)
Ockonal