为什么我需要重载= [英] Why do I need to overload =

查看:59
本文介绍了为什么我需要重载=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图用一个耗时的操作转换一个类,通过

添加一个线程。为了能够将数据传递给线程我已经声明了一个

类ThreadParam,如下所示:


class CPROvisioning

{

public:

struct ThreadParam

{

ThreadParam(CProvisioning * a_pThis,const CString& a_strConfig,
CString& a_strXmlOut):

pThis(a_pThis),

strConfig(a_strConfig),

strOut(a_strXmlOut){}

CPROvisioning * pThis;

const CString& strConfig;

CString& strOut;

};


CPROvisioning(无效);

~CPROvisioning(无效);

静态DWORD ProvisioningThr(LPVOID pvarg);

DWORD ProvisioningThr(const CString& strConfig,CString& strXmlOut);


BOOL ProcessConfigXML(const CString& strConfig,CString& strOut);

受保护:


ThreadParam m_thrParam;

HANDLE m_hProvThread;

};


CProvisioning :: CProvisioning(无效):

m_thrParam(这,CString(_T(""))),CString( _T(""))),

m_hProvThread(NULL)

{

}


ProcessConfig方法接收两个字符串,一个包含配置

文件,另一个包含结果:

BOOL CProvisioning :: ProcessConfigXML(const CString& strConfig,CString& ;

strXmlOut)

{

DWORD dwRet = 0;


//开始一个帖子

m_thrParam = ThreadPa ram(this,strConfig,strXmlOut);

m_hProvThread = CreateThread(

NULL,

0,

& ; CPROvisioning :: ProvisioningThr,

& m_thrParam,

0,NULL);

...

}


/ * static * /

DWORD CPROvisioning :: ProvisioningThr(LPVOID pvarg)

{

ThreadParam * pThrParam = static_cast< ThreadParam *>(pvarg);

if(pThrParam){

CPROvisioning * pThis = pThrParam-> pThis;

返回pThis-> ProvisioningThr(pThis-> m_thrParam.strConfig,

pThis-> m_thrParam.strOut);

}


返回-1;

}

DWORD CProvisioning :: ProvisioningThr(const CString& strConfig,CString&

strXmlOut)

{

//一些冗长的操作...


返回0;

}

我得到的问题是m_thrParam = ThreadParam(this,strConfig,

strXmlOut);因为我得到:


错误C2582:''operator =''功能不可用

''CProvisioning :: ThreadParam''

1)首先我不明白为什么我需要超载=

2)我该如何解决?





Hi,

I am trying to transform a class with some time consuming operation by
adding a thread. To be able to pass data to thread I have declared a
class ThreadParam as shown below :

class CProvisioning
{
public:
struct ThreadParam
{
ThreadParam(CProvisioning* a_pThis, const CString& a_strConfig,
CString& a_strXmlOut):
pThis(a_pThis),
strConfig( a_strConfig ),
strOut( a_strXmlOut ) {}
CProvisioning* pThis;
const CString& strConfig;
CString& strOut;
};

CProvisioning(void);
~CProvisioning(void);

static DWORD ProvisioningThr(LPVOID pvarg);
DWORD ProvisioningThr(const CString& strConfig, CString& strXmlOut);

BOOL ProcessConfigXML(const CString& strConfig, CString& strOut);
protected:

ThreadParam m_thrParam;
HANDLE m_hProvThread;
};

CProvisioning::CProvisioning(void):
m_thrParam(this, CString(_T("")), CString(_T(""))),
m_hProvThread(NULL)
{
}

The ProcessConfig method receives two strings, one holding the config
file and the other to put the result:
BOOL CProvisioning::ProcessConfigXML(const CString& strConfig, CString&
strXmlOut)
{
DWORD dwRet = 0;

// Start a thread
m_thrParam = ThreadParam(this, strConfig, strXmlOut);
m_hProvThread = CreateThread(
NULL,
0,
&CProvisioning::ProvisioningThr,
&m_thrParam,
0, NULL);
...
}

/*static*/
DWORD CProvisioning::ProvisioningThr(LPVOID pvarg)
{
ThreadParam* pThrParam = static_cast<ThreadParam*>(pvarg);
if (pThrParam) {
CProvisioning* pThis = pThrParam->pThis;
return pThis->ProvisioningThr(pThis->m_thrParam.strConfig,
pThis->m_thrParam.strOut);
}

return -1;
}
DWORD CProvisioning::ProvisioningThr(const CString& strConfig, CString&
strXmlOut)
{
// Some lengthy operations ...

return 0;
}
The problem I get is with m_thrParam = ThreadParam(this, strConfig,
strXmlOut); because I get :

error C2582: ''operator ='' function is unavailable in
''CProvisioning::ThreadParam''
1) First I don''t understand why I need to overload =
2) How can I fix it ?





推荐答案

John Doe写道:
John Doe wrote:

我试图改造

添加线程的一个耗时操作的类。为了能够将数据传递给线程我已经声明了一个

类ThreadParam,如下所示:

[..]

struct ThreadParam

{

ThreadParam(CProvisioning * a_pThis,const CString& a_strConfig,

CString& a_strXmlOut):

pThis(a_pThis ),

strConfig(a_strConfig),

strOut(a_strXmlOut){}


CPROvisioning * pThis;

const CString& strConfig;

CString& strOut;

};

[...]


我得到的问题是m_thrParam = ThreadParam(this,strConfig ,

strXmlOut);因为我得到:


错误C2582:''operator =''功能不可用

''CProvisioning :: ThreadParam''


1)首先我不明白为什么我需要重载=
I am trying to transform a class with some time consuming operation by
adding a thread. To be able to pass data to thread I have declared a
class ThreadParam as shown below :
[..]
struct ThreadParam
{
ThreadParam(CProvisioning* a_pThis, const CString& a_strConfig,
CString& a_strXmlOut):
pThis(a_pThis),
strConfig( a_strConfig ),
strOut( a_strXmlOut ) {}
CProvisioning* pThis;
const CString& strConfig;
CString& strOut;
};
[...]

The problem I get is with m_thrParam = ThreadParam(this, strConfig,
strXmlOut); because I get :

error C2582: ''operator ='' function is unavailable in
''CProvisioning::ThreadParam''
1) First I don''t understand why I need to overload =



编译器因为你有引用成员而无法生成一个。 br />
一旦初始化(在构造期间),就无法重新设置引用。

The compiler cannot generate one because you have reference members.
Once initialised (during construction), a reference cannot be reseated.


2)我该如何解决?
2) How can I fix it ?



您需要重载赋值运算符并决定如何处理

''strConfig''成员(例如,不管它)和''strOut''会员。

*运营商的示例*可能如下所示:


struct ThreadParam

$

...

ThreadParam& operator =(ThreadParam const& other)

{

pThis = other.pThis;

return * this;

}

};


V

-

请删除大写''A'在回复电子邮件的时候

我没有回复最热门的回复,请不要问

You need to overload the assignment operator and decide what to do with
the ''strConfig'' member (e.g. leave it alone) and the ''strOut'' member.
*An example* of your operator= might look like this:

struct ThreadParam
{
...
ThreadParam& operator=(ThreadParam const& other)
{
pThis = other.pThis;
return *this;
}
};

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

John Doe写道:
John Doe wrote:

>我试图用一些时间来改造一个类通过添加线程来操作。为了能够将数据传递给线程我已经声明了一个
类ThreadParam,如下所示:
[...]
struct ThreadParam
{
ThreadParam(CProvisioning * a_pThis ,const CString&
a_strConfig,CString& a_strXmlOut):
pThis(a_pThis),
strConfig(a_strConfig),
strOut(a_strXmlOut){}

CPROvisioning * pThis;
const CString& strConfig;
CString& strOut;
};
[...]

我得到的问题是m_thrParam = ThreadParam(this,strConfig,
strXmlOut);因为我得到:

错误C2582:''operator =''功能不可用
''CProvisioning :: ThreadParam''

1)首先我不要我不明白为什么我需要重载=
>I am trying to transform a class with some time consuming operation by
adding a thread. To be able to pass data to thread I have declared a
class ThreadParam as shown below :
[..]
struct ThreadParam
{
ThreadParam(CProvisioning* a_pThis, const CString&
a_strConfig, CString& a_strXmlOut):
pThis(a_pThis),
strConfig( a_strConfig ),
strOut( a_strXmlOut ) {}
CProvisioning* pThis;
const CString& strConfig;
CString& strOut;
};
[...]

The problem I get is with m_thrParam = ThreadParam(this, strConfig,
strXmlOut); because I get :

error C2582: ''operator ='' function is unavailable in
''CProvisioning::ThreadParam''
1) First I don''t understand why I need to overload =



编译器无法生成一个,因为你有参考成员。

一旦初始化(在施工期间) ),参考不能重新安装。


The compiler cannot generate one because you have reference members.
Once initialised (during construction), a reference cannot be reseated.


> 2)我该如何解决?
>2) How can I fix it ?



您需要重载赋值运算符并决定如何处理

''strConfig''成员(例如,不管它)和''strOut''会员。

*运营商的示例*可能如下所示:


struct ThreadParam

$

...

ThreadParam& operator =(ThreadParam const& other)

{

pThis = other.pThis;

return * this;

}

};


V


You need to overload the assignment operator and decide what to do with
the ''strConfig'' member (e.g. leave it alone) and the ''strOut'' member.
*An example* of your operator= might look like this:

struct ThreadParam
{
...
ThreadParam& operator=(ThreadParam const& other)
{
pThis = other.pThis;
return *this;
}
};

V



问题是我跑了一次:


m_thrParam = ThreadParam(this,strConfig,strXmlOut);

strConfig是一个有效的引用,但是strXmlOut是NULL ...

The problem is once I run :

m_thrParam = ThreadParam(this, strConfig, strXmlOut);
strConfig is a valid reference but strXmlOut is NULL ...


John Doe写道:
John Doe wrote:

Victor Bazarov写道:
Victor Bazarov wrote:

> John Doe写道:
>John Doe wrote:

>>我试图通过添加一个线程来转换一个耗时的操作类。为了能够将数据传递给线程我已经声明了一个ThreadParam类,如下所示:
[...]
struct ThreadParam
{
ThreadParam(CProvisioning * a_pThis ,const CString&
a_strConfig,CString& a_strXmlOut):
pThis(a_pThis),
strConfig(a_strConfig),
strOut(a_strXmlOut){}

CPROvisioning * pThis;
const CString& strConfig;
CString& strOut;
};
[...]

我得到的问题是m_thrParam = ThreadParam(this,strConfig,
strXmlOut);因为我得到:

错误C2582:''operator =''功能不可用
''CProvisioning :: ThreadParam''

1)首先我不要我不明白为什么我需要重载=
>>I am trying to transform a class with some time consuming operation
by adding a thread. To be able to pass data to thread I have declared
a class ThreadParam as shown below :
[..]
struct ThreadParam
{
ThreadParam(CProvisioning* a_pThis, const CString&
a_strConfig, CString& a_strXmlOut):
pThis(a_pThis),
strConfig( a_strConfig ),
strOut( a_strXmlOut ) {}
CProvisioning* pThis;
const CString& strConfig;
CString& strOut;
};
[...]

The problem I get is with m_thrParam = ThreadParam(this, strConfig,
strXmlOut); because I get :

error C2582: ''operator ='' function is unavailable in
''CProvisioning::ThreadParam''
1) First I don''t understand why I need to overload =


由于你有引用成员,编译器无法生成一个。
一旦初始化(在构造期间),就无法重新引用引用。


The compiler cannot generate one because you have reference members.
Once initialised (during construction), a reference cannot be reseated.


>> 2)我该如何解决?
>>2) How can I fix it ?


你需要重载赋值运算符并决定用''strConfig''成员做什么
(例如不管它)和''strOut''<成员。 *运营商的示例*可能如下所示:

struct ThreadParam
{
...
ThreadParam& operator =(ThreadParam const& other)
{
pThis = other.pThis;
return * this;
}
};

V


You need to overload the assignment operator and decide what to do
with the ''strConfig'' member (e.g. leave it alone) and the ''strOut''
member. *An example* of your operator= might look like this:

struct ThreadParam
{
...
ThreadParam& operator=(ThreadParam const& other)
{
pThis = other.pThis;
return *this;
}
};

V



问题是我跑了一次:


m_thrParam = ThreadParam(this,strConfig,strXmlOut);

strConfig是一个有效的引用,但是strXmlOut是NULL ...

The problem is once I run :

m_thrParam = ThreadParam(this, strConfig, strXmlOut);
strConfig is a valid reference but strXmlOut is NULL ...



我想我错过了一个重要答案:

一次初始化(在施工期间),参考不能重新安装。


所以我想做的是不可能......

I think I miss one important answer :
Once initialised (during construction), a reference cannot be reseated.

So what I want to do is not possible...


这篇关于为什么我需要重载=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆