In the header, I have
class CSomeClass
{
const GUID m_guid;
public:
CSomeClass();
///...
}
And in the source file
CSomeClass::CSomeClass()
, m_guid(
[]() {
GUID g;
::CoCreateGuid(&g);
return g;
}()
)
{
}
As you know Guids can be used as identifications not meant to be changed. Given the ::CocreateGuid()
function provides what I want as an output parameter, instead of returning it, I cannot use directly a simple call to the function for initializing the m_guid member field, that is constant.
So, a consequence of its constness, is that it must be initialized before the opening bracket in initializer list, and therefore not be simply assigned with a call to ::CocreateGuid()
in the constructor body.
Is there a simpler way to initialize it than this lambda expression?
const
already kicks in in the constructor body. I'm with @Jarod42 - writeprivate: static GUID CSomeClass::MakeGUID()
and use that to initializem_guid
– Igor Tandetnikprivate static
member function I would completely hide it in the source file (.cpp). Make it eitherstatic
in the .cpp or put it in an anonymousnamespace
also in the .cpp. – Cassio Neri