I have a template class with private constructor and destructor:
template <typename T>
class Client
{
private:
Client(...) {}
~Client() {}
template <typename U>
friend class Client<T>& initialize(...);
};
template <typename T>
Client<T> initialize(...)
{
Client<T> client = new Client<T>(...);
}
I’m not sure of the correct syntax for the friend. Can anybody help?
Ignoring the ellipses (which I assume mean “a bunch of parameters that aren’t relevant to the question”), this should work:
Notice that the friend declaration is essentially the same as the function declaration but with the
friendkeyword prefixed before the return type.Also, the destructor is public so that users of
initialize()will be able to destruct the returned instance ofClient<>. The constructor is still private so onlyinitialize()can create instances of it.The above code should allow this to work: