1

因此,我正在尝试将大多数教程提供的以下 C++ thrift 服务器实现转换为类形式:

int port = 9090;
  shared_ptr<SomethingHandler> handler(new SomethingHandler());
  shared_ptr<TProcessor> processor(new SomethingProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);

为此,我创建了所有 shared_ptrs 和 TSimpleServer 成员,但我在初始化分配方面遇到了麻烦。

在初始化列表中构造 shared_ptr 似乎会导致 badmalloc 错误,因此我的解决方法是将它们声明为成员并通过分配进行初始化:

 handler = shared_ptr<type>(new Type). 

这里的问题是 TSimpleServer 没有赋值运算符或构造函数采用 void,因此它需要在构造函数时构造它所依赖的 shared_ptrs 以便可以构造它。
有什么想法我在这里想念的吗?

更多伪代码:

class myclass {
 int port;
 shared_ptr<MyHandlder> handler;
 shared_ptr<TProcessor> processor;
 shared_ptr<TServerTransport> serverTransport;
 shared_ptr<TTransportFactory>  transportFactory;
 shared_ptr<TProtocolFactory> protocolFactory;
 TSimpleServer server;

  public: 
        explicit myclass(): port(9090), handler(new MyHandler()), processor(new MyProcessor(handler)), serverTransport(new TServerSocket(port)), transportFactory(new TBufferedTransportFactory()), protocolFactory(new TBinaryProtocolFactory()), server(processor, serverTransport, transportFactory, protocolFactory) { }

 int start () { server.serve() }
4

0 回答 0