Search code examples
c++classconstructorhazelcast

How to initialize an object with private object member which constructor eat non-const reference as a parameter


I've ask this question, and I still can't solve the problem on my project.

My project uses third-party library, Hazelcast C++ Client.

The use of Hazelcast Client is like:

#include "hazelcast/client/HazelcastAll.h"

using namespace hazelcast::client;

int main(){
    ClientConfig clientConfig;
    Address address("192.168.85.34", 5701);
    clientConfig.getNetworkConfig().addAddress(address);
    HazelcastClient hz(clientConfig);

    // do something...

    hz.shutdown();
    return 0;
}

This code is no problem. More examples can be gotten in its website. However, if I want to make HazelcastClient hz as a class private member, I have no idea to instantiate my class !!

I've checked out the HazelcastClient constructor in HazelcastClient.h header, the constructor use ClientConfig &config as parameter, which let the following code cause error like:

HzService.cpp: error: cannot bind non-const lvalue reference of type ‘hazelcast::client::ClientConfig&’ to an rvalue of type ‘hazelcast::client::ClientConfig’

It's because the compilation message has 14282 lines, which is too many to paste on here, so I just paste an important error above! The full compilation message can be download here.

#include "hazelcast/client/HazelcastAll.h"
using namespace hazelcast::client;

class HzService {
public:
    HzService();
    virtual ~HzService();

private:
    // Variables:
    HazelcastClient hz_client;

    // Functions:
    ClientConfig createLocalConfig();
};

HzService::HzService()
    :hz_client(createLocalConfig()){
}

HzService::~HzService() {
    hz_client.shutdown();
}

ClientConfig HzService::createLocalConfig() {
    ClientConfig clientConfig;
    Address address("192.168.85.34", 5701);
    clientConfig.getNetworkConfig().addAddress(address);
    return clientConfig;
}

int main () { 
    HzService hs;
    return 0;
}

Maybe that's not the Hazelcast problem, but the question if I use third-party library that its class constructor use non-const reference as parameters, how can I make it into my class private member?

If the issue is specifically related to Hazelcast, I'll change the title.



EDIT:

If someone need to test the code, please download the Hazelcast C++ Client first, and use following command to compile (The content of test.cpp is the code above) :

g++ -std=c++11 \
    -I/path/to/hazelcast-cpp-client/hazelcast/include \
    -I/path/to/hazelcast-cpp-client/external/include/ \
    -I/path/to/hazelcast-cpp-client/hazelcast/generated-sources/include \
    test.cpp \
    /path/to/hazelcast-cpp-client/release/libHazelcastClient3.11_64.a \
    -lpthread

Solution

  • Declare hz_client as a pointer instead.
    Declare ClientConfig as a pointer as well

    In HzService() first create the clientconfig instance, then create hz_client passing the instance of clientconfig into it.

    E.g.

    class HzService
    {
    ...
    HazelcastClient* hz = nullptr;  // but better with smart pointer and custom deleter.
    std::unique_ptr<ClientConfig> cc;
    ...
    
    HzService()
    {
      cc = std::make_unique<ClientConfig>();  
      Address address("192.168.85.34", 5701);
      cc->getNetworkConfig().addAddress(address);
      hz_client = new HazelcastClient(*cc.get());
    }
    

    then add some code in the destructor to do shutdown and delete hz_client