变量复制问题,用于多线程
VARIANT是否具有默认复制功能,还是我必须编写复制功能和替代运算符=
https://docs.microsoft.com/en-us/windows/desktop/winauto/variant-structure
https://docs.microsoft.com/en-us/windows/desktop/api/oaidl/ns-oaidl-tagvariant
答案 0 :(得分:1)
可以吗?
class Data()
{
protected:
VARIANT m_var;
public:
Data()
{
VariantInit(&m_var);
}
~Data()
{
VariantClear(&m_var);
}
Data(const Data& other)
{
VariantInit(&m_var);
VariantCopy(&m_var, &other.m_var);
}
Data& operator = (const Data& other)
{
if(this == &other)
return *this;
VariantInit(&m_var);
VariantCopy(&m_var, &other.m_var);
return *this;
}
Data(Data&& other)
{
VariantInit(&m_var);
VariantCopy(&m_var, &other.m_var);
VariantClear(&other.m_var);
}
Data& operator = (Data&& d)
{
if(this == &other)
return *this;
VariantInit(&m_var);
VariantCopy(&m_var, &other.m_var);
VariantClear(&other.m_var);
return *this;
}
}
答案 1 :(得分:0)
我认为您不了解C ++复制和引用语义。这是一个示例,您可以在三种不同类型的参数调用中使用_variant_t代替VARIANT:复制,引用,指针。
#include <windows.h>
#include <comdef.h>
#include <iostream>
using namespace std;
void DoSomething(VARIANT v)
{
_variant_t vConv;
VariantChangeType(&vConv, &v, 0, VT_BSTR);
wcout << vConv.bstrVal << endl;
}
void DoSomethingWithRef(const VARIANT& v)
{
_variant_t vConv;
VariantChangeType(&vConv, &v, 0, VT_BSTR);
wcout << vConv.bstrVal << endl;
}
void DoSomethingWithPtr(const VARIANT* pv)
{
_variant_t vConv;
VariantChangeType(&vConv, pv, 0, VT_BSTR);
wcout << vConv.bstrVal << endl;
}
int main(int argc, char* argv[])
{
_variant_t v_t(123L);
DoSomething(v_t);
DoSomethingWithRef(v_t);
DoSomethingWithPtr(&v_t);
return 0;
}