相关疑难解决方法(0)

我应该如何在C++中正确使用FormatMessage()?

没有:

  • MFC
  • ATL

如何使用FormatMessage()获取错误文本HRESULT

 HRESULT hresult = application.CreateInstance("Excel.Application");

 if (FAILED(hresult))
 {
     // what should i put here to obtain a human-readable
     // description of the error?
     exit (hresult);
 }
Run Code Online (Sandbox Code Playgroud)

c++ windows error-handling formatmessage

84
推荐指数
5
解决办法
5万
查看次数

比较运算符==中的共享指针常量

我偶然发现了我正在使用的共享指针的意外行为.

共享指针实现引用计数和分离(例如,复制),如果需要,包含非const使用的实例.
为实现此目的,对于每个getter函数,智能指针都有一个const和一个non-const版本,例如:operator T *()operator T const *() const.

问题:比较指针值nullptr导致分离.

预期:我认为比较运算符总是会调用const版本.

简化示例:(
此实现没有引用计数,但仍显示问题)

#include <iostream>

template<typename T>
class SharedPointer
{
public:
    inline operator T *() { std::cout << "Detached"; return d; }
    inline operator const T *() const { std::cout << "Not detached"; return d; }
    inline T *data() { std::cout << "Detached"; return d; }
    inline const T *data() const { std::cout << "Not detached"; …
Run Code Online (Sandbox Code Playgroud)

c++ const shared-ptr comparison-operators

0
推荐指数
1
解决办法
91
查看次数