如何把面向过程的程序提取为一个类
代码如下: 求大神帮助 谢谢!
#include "stdafx.h"
#include <boost/smart_ptr.hpp>
#include <../tunk_api/SmartFox.h>
#include <../tunk_api/Requests/LoginRequest.h>
boost::shared_ptr<Sfs2X::SmartFox> ptrSmartFox;
static void OnConnection(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
static void OnLogin(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
static void OnLoginError(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
int _tmain(int argc, _TCHAR* argv[])
{
ptrSmartFox = boost::shared_ptr<Sfs2X::SmartFox>(new Sfs2X::SmartFox(true));
ptrSmartFox->ThreadSafeMode(false);
ptrSmartFox->Connect("127.0.0.1", 9933);
ptrSmartFox->AddEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(OnConnection, (unsigned long long)_tmain)));
ptrSmartFox->AddEventListener(SFSEvent::LOGIN, boost::shared_ptr<EventListenerDelegate> ( new EventListenerDelegate(OnLogin, (unsigned long long)_tmain)));
ptrSmartFox->AddEventListener(SFSEvent::LOGIN_ERROR, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(OnLoginError, (unsigned long long)_tmain)));
cin.get();
return 0;
}
void OnConnection(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
// Get the pointer to my class
boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();
boost::shared_ptr<void> ptrEventParamValueSuccess = (*ptrEventParams)["success"];
boost::shared_ptr<bool> ptrValue = (boost::static_pointer_cast<bool>)(ptrEventParamValueSuccess);
if (*ptrValue == true)
{
cout<<"Connection was established"<<endl;
boost::shared_ptr<IRequest> request (new LoginRequest("user","1111","BasicExamples"));
ptrSmartFox->Send(request);
}
else
{
cout<<" Connection failed"<<endl;
}
}
void OnLogin(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
cout<<" Login success"<<endl;
}
void OnLoginError(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();
boost::shared_ptr<void> ptrEventParamValueErrorMessage = (*ptrEventParams)["errorMessage"];
boost::shared_ptr<string> ptrErrorMessage = ((boost::static_pointer_cast<string>)(ptrEventParamValueErrorMessage));
string* message = new string("Login failure: " + (*ptrErrorMessage));
}
[解决办法]
//大概是这样吧:
class TMySmartFox
{
boost::shared_ptr<Sfs2X::SmartFox> ptrSmartFox;
static void OnConnection(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
static void OnLogin(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
static void OnLoginError(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
public :
TMySmartFox()
{
ptrSmartFox = boost::shared_ptr<Sfs2X::SmartFox>(new Sfs2X::SmartFox(true));
ptrSmartFox->ThreadSafeMode(false);
ptrSmartFox->Connect("127.0.0.1", 9933);
ptrSmartFox->AddEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(OnConnection, (unsigned long long)_tmain)));
ptrSmartFox->AddEventListener(SFSEvent::LOGIN, boost::shared_ptr<EventListenerDelegate> ( new EventListenerDelegate(OnLogin, (unsigned long long)_tmain)));
ptrSmartFox->AddEventListener(SFSEvent::LOGIN_ERROR, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(OnLoginError, (unsigned long long)_tmain)));
}
};
然后实现一下几个函数。如何封装最好,还是要看该类的运行场景的。
[解决办法]
你上面代码的运行模式,类似于单件模式,你好好看一下单件模式。这里说,不如你去看书。
[解决办法]
先找书,看明白单件模式的代码吧。