Title: C++ 0x11 - st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0bind and composition
Tags: c++
Question: I want to bind member class event to consumer class function while using composition. Please correct me if description of problem is wrong, I come from C# background.
Here is the code:
#include "stdafx.h"
#include <functional&gt;
#include <iostream&gt;
using namespace st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0placeholders;
class Observable
public:
void Register(st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0function<void()&gt; Callback)
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0function<void()&gt; OnStateChanged;
void ChangeState()
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "DevicioClient75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0hangeState" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
OnStateChanged();
class Observer
Observable client;
void OnStateChanged()
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "DevicioConsumer75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
public:
void Run()
// does work
client.OnStateChanged = []{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "client.OnStateChanged" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
};
// doesn't work
//  OnStateChanged = st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0bind(&amp;Observabl75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged, this);
client.ChangeState();
int _tmain(int argc, _TCHAR* argv[])
Observer consumer;
consumer.Run();
getchar();
return 0;
I want to run Observer75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged when Observable call OnStateChanged function.
Comment: why _tmain instead of main? Why _TCHAR instead of char?

Comment: @ChristianHackl "stdafx.h" has to do with [precompiled headers](http://stackoverflow.com/a/4726838/5420829)

Comment: If you're looking for a robust simulacrum to c# closures and automatic reference counting, you may want to bind your function to a `shared_ptr` or a lambda that strengthens a `weak_ptr` to your observer rather than simply `this`. An excellent library is of course boost75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0signals2, which is also thread-safe and offers complete reference counting support through the `track_foreign` member function.

Comment: _tmain _TCHAR and #include "stdafx.h" were added by by visual studio when i created example console application. Should i remove them? Is visual studio wrong?

Comment: You want `st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0bind(&Observabl75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged, &client)`.

Comment: @АндрейБеньковский: Not to mention `#include "stdafx.h"`...

Comment: @АндрейБеньковский: I know. What do precompiled headers have to do with the question?

Here is the accepted answer: It says &amp;Observabl75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged for the function you tried to bind... did you mean &amp;Observer75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged? Because this works ok:
client.OnStateChanged = st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0bind(&amp;Observer75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0OnStateChanged, this);


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:11/01/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++>', 'Title': 'C++ 0x11 - std::bind and composition', 'ViewCount': 260, 'id': 33460700, 'user_AboutMe': '', 'user_AccountId': 881958.0, 'user_DisplayName': 'toomasz', 'user_Reputation': 35.0, 'user_Views': 5.0, 'user_id': 1646763.0}}