我最近一直在用 C++11 智能指针替换我所有的原始指针,现在我终于完成了,除了我的 DirectX 长指针。
我想按照下面的思路实现一些东西:
std::shared_ptr<IDirect3D9> p_d3d;
p_d3d( Direct3DCreate9(D3D_SDK_VERSION), [](IDirect3D9 *p) {//smart pointer initialization(1)
p->Release();
});
我从 visual studio 收到以下错误:
error : call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
与 CComPtr 相比,我更愿意使用 std::unique_ptr 或 std::shared_ptr。 关于为什么会发生这种情况以及可能的实现方法有什么想法吗?
最佳答案
你似乎在你的 shared_ptr 上调用 operator()。这不是你构建 shared_ptr 的方式。尝试使用重置,或在声明变量的同一行构造指针。
请注意,您调用的创建函数会为您提供一个引用计数为 1 的指针。
关于directx 的 C++ 智能指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14017050/