Disallowing creation of the temporary objects

后端 未结 8 938
南方客
南方客 2020-11-29 09:01

While debugging crash in a multithreaded application I finally located the problem in this statement:

CSingleLock(&m_criticalSection, TRUE);
8条回答
  •  悲&欢浪女
    2020-11-29 09:57

    What about the following? Slightly abuses the preprocessor, but it's clever enough that I think it should be included:

    class CSingleLock
    {
        ...
    };
    #define CSingleLock class CSingleLock
    

    Now forgetting to name the temporary results in an error, because while the following is valid C++:

    class CSingleLock lock(&m_criticalSection, true); // Compiles just fine!
    

    The same code, but omitting the name, is not:

    class CSingleLock(&m_criticalSection, true); // <-- ERROR!
    

提交回复
热议问题