简体   繁体   English

声明全局变量,在方法内初始化

[英]Declare Global Variable, Initialize within Method

I am creating a thrift client, which has a bunch of overhead: a TSocket , a TBufferedTransport , and a TBinaryProtocol . 我正在创建一个节俭的客户端,它有很多开销: TSocketTBufferedTransportTBinaryProtocol Usually it is created like this 通常是这样创建的

Example Thrift Client Thrift客户示例

int main(int argc, char **argv) {
    shared_ptr<TTransport> mysocket(new TSocket("localhost", 9090));
    shared_ptr<TTransport> mytransport(new TBufferedTransport(mysocket));
    shared_ptr<TProtocol> myprotocol(new TBinaryProtocol(mytransport));
    TestServiceClient client(myprotocol);

    try {
        mytransport->open();
        client.testmethod();  //and you can call server methods like so
        mytransport->close();
    }catch (TException &tx) {
        printf("ERROR: %s\n", tx.what());
    }
}

However, for my client, I want it in the form of a static library. 但是,对于我的客户,我希望它以静态库的形式出现。 Thus, there is no main method, and I created init() and exit() methods to open and close mytransport . 因此,没有main方法,因此我创建了init()exit()方法来打开和关闭mytransport That also meant I had to make mysocket , mytransport , myprotocol , and client all global variables, but I also want to set mysocket to an ip other than "localhost", which means initializing mysocket inside a method. 这也意味着我不得不做出mysocketmytransportmyprotocolclient所有的全局变量,但我也想设置mysocket比“localhost”的其他的IP,这意味着初始化mysocket的方法中。 Any ideas? 有任何想法吗?

PS. PS。 Also, does anybody know how I can call init() and exit() automatically when the static library is opened and closed? 另外,有人知道在打开和关闭静态库时如何自动调用init()exit()吗? [SOLVED] [解决了]

edit: I forgot to mention that I can't initialize mysocket until I know the correct ip, so I don't think I can declare mysocket as a global variable anyways 编辑:我忘了提一下,直到我知道正确的ip才能初始化mysocket ,所以我认为无论如何我都不能将mysocket声明为全局变量

After reading the question more thoroughly, I think this may be more along the lines of what you are looking for: 在更彻底地阅读了问题之后,我认为这可能更符合您的需求:

mylib.h mylib.h

class libClosure{
  public:
  libClosure();
  ~libClosure();
};

mylib.cpp mylib.cpp

#include "mylib.h"
#include <iostream>

using namespace std;

libClosure::libClosure(){ cout << "call open()" << endl;}
libClosure::~libClosure(){ cout << "call close()" << endl;}

libClosure __lc;

main.cpp main.cpp中

#include "mylib.h"
#include <iostream>

using namespace std;

int main(){
  cout << "client.testmethod()" << endl;
  return 0;
}

output 产量

call open()
client.testmethod()
call close()

Constructing an initializing object within an implementation file is a common way to achieve exactly what you're looking for. 在实现文件中构造一个初始化对象是实现所需内容的一种常用方法。 Essentially, the constructor for class libClosure becomes your entry point, and the destructor for class libClosure becomes your exit point. 本质上, class libClosure的构造class libClosure成为您的入口点,而class libClosure的析构函数成为您的出口点。

The reason this works is because all implementation files for the headers must construct their global variables before main() is run. 之所以起作用,是因为在运行main()之前,标头的所有实现文件都必须构造其全局变量。 You can't guarantee that one implementation file will be constructed/destructed before another, but you know that all objects in implementation files must be constructed before the main() at some point, and destructed after the main() at some point. 你不能保证一个实现文件将建造/前另一破坏,但你知道,在执行文件中的所有对象必须在之前建造main()在某些时候,和破坏后main()在某一时刻。

I ended up solving it myself. 我最终自己解决了。 Even though using static local variables like Aggieboy suggested seems perfectly valid, I think this is a little simpler: 即使使用像Aggieboy建议的静态局部变量似乎完全有效,但我认为这要简单一些:

barebones version 准系统版本

shared_ptr<TTransport> mysocket(createTSocket());

TSocket* createTSocket() {
    //init stuff goes here, for example
    string ip = "localhost"
    int port = 9090

    return new TSocket(ip,port)
}

the full code (well, most of it) 完整的代码(嗯,大部分)

class ThriftProxy {
public:
    ThriftProxy() :
        trans(createTSocket()),
        proto(new TBinaryProtocol(trans)),
        client(proto)
    {
        trans->open();
    }

    TSocket* createTSocket() {
        //default: ip = "localhost", port = 9090
        string ip = "localhost";
        int port = 9090;

                    //get ip and port from a text file

        return new TSocket(ip, port);
    }

    ~ThriftProxy()
    {
        trans->close();
    }

private:
    boost::shared_ptr<TSocket> trans;
    boost::shared_ptr<TProtocol> proto;
public:
    XtkServiceClient client;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM