错误的构造函数被调用 [英] Wrong Constructor Called

查看:85
本文介绍了错误的构造函数被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个错误的构造函数被调用的情况。我用
定义了2个不同参数类型的构造函数,这些构造函数的定义如下:


class __declspec(dllexport) CColumn:public CColumnBase

{

public:


CColumn(CString columnType,CObject * aOwner,CString anId);

CColumn(CString columnType,CObject * aOwner,bool batchUpdated);

....

}


这些函数的实现看起来像这样...

CColumn :: CColumn(CString columnType,CObject * aOwner,CString anId)

{

setColumnType(columnType);

setOwner(aOwner);

setId(anId);

setLength(10) ;

setPrecision(5);

setField();

setBatchUpdated(false);

}


CColumn :: CColumn(CString columnType,CObject * aOwner,bool

batchUpdated)

{

setColumnType(columnType);

setOwner(aOwner);

setId("&qu ot;);

setLength(10);

setPrecision(5);

setField();

setBatchUpdated(batchUpdated);

}


当调用CDual()构造函数中的下一行时,

构造函数调用CColumn :: CColumn(CString columnType,

CObject * aOwner,bool batchUpdated)的签名,而不是我想要的那个



//////////////////////////////////////////////// //////////////////////

//施工/毁坏

/////// ////////////////////////////////////////////////// /////////////

IMPLEMENT_DYNCREATE(CDual,CDomain)


CDual :: CDual()

{

...

addColumn(新CColumn(TIMESTAMP_COLUMN,this," TIMESTAMP"));

...

}


有没有人知道为什么会这样,以及如何避免这个问题?


谢谢,


Joe

I have a situation where the wrong constructor is being called. I
have defined 2 constructors with different parameter types that are
defined as follows...

class __declspec(dllexport)CColumn : public CColumnBase
{
public:

CColumn(CString columnType,CObject *aOwner, CString anId);
CColumn(CString columnType,CObject *aOwner, bool batchUpdated);
....
}

The implementation of these functions looks like this...

CColumn::CColumn(CString columnType, CObject *aOwner, CString anId)
{
setColumnType(columnType);
setOwner(aOwner);
setId(anId);
setLength(10);
setPrecision(5);
setField();
setBatchUpdated(false);
}

CColumn::CColumn(CString columnType, CObject *aOwner, bool
batchUpdated)
{
setColumnType(columnType);
setOwner(aOwner);
setId("");
setLength(10);
setPrecision(5);
setField();
setBatchUpdated(batchUpdated);
}

When the line below in the CDual() constructor gets called, the
constructor with the signature of CColumn::CColumn(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE( CDual, CDomain )

CDual::CDual()
{
...
addColumn(new CColumn(TIMESTAMP_COLUMN,this,"TIMESTAMP"));
...
}

Does anyone have any idea why this is happening and how to avoid this
issue?

Thanks,

Joe

推荐答案



" Joe" < JR ***** @ purina.com>在消息新闻中写道:ec ************************** @ posting.google.c om ...

"Joe" <jr*****@purina.com> wrote in message news:ec**************************@posting.google.c om...
当调用CDual()构造函数中的下一行时,将调用具有CColumn :: CColumn(CString columnType,
CObject * aOwner,bool batchUpdated)签名的
构造函数,而不是<我希望。
When the line below in the CDual() constructor gets called, the
constructor with the signature of CColumn::CColumn(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.




因为bool比chartring更适合char *。任何指针

都可以转换为bool,这是一个标准的转换序列。

我假设CString有一个带有char *的转换构造函数。这个

是用户定义的转换序列。标准转换序列

胜出。


你要么必须使你的重载不那么模糊,要么明确地做

调用的东西使它与bool不匹配(比如自己将其转换为CString)。

新的CColumn(TIMESTAMP_COLUMN,这个,CString(&#TIMESTAMP"));



Because the bool is a better match for char* than CString is. Any pointer
can be converted to bool, and that is a standard conversion sequence.
I assume CString has a converting constructor that takes a char*. This
is a user-defined conversion sequence. The standard conversion sequence
wins out.

You''ll either have to make your overloads less ambiguous, or explicitly do
something to the call to make it not match bool (like converting it to CString yourself).
new CColumn(TIMESTAMP_COLUMN, this, CString("TIMESTAMP"));


Joe写道:


CDual :: CDual()
{
...
addColumn(新的CColumn(TIMESTAMP_COLUMN,这个,TIMESTAMP));
...
}

有谁知道为什么会发生这种情况


我不确定这是否是一个编译器错误。

但显然编译器会从一个角色预先转换

指向建筑物上的bool指示

的临时物体。

以及如何避免这个
问题?


CDual::CDual()
{
...
addColumn(new CColumn(TIMESTAMP_COLUMN,this,"TIMESTAMP"));
...
}

Does anyone have any idea why this is happening
I am not sure if this is a compiler bug or not.
But obviously the compiler prevers the conversion
from a character pointer to a bool over the construction
of a temporary object.
and how to avoid this
issue?



简单。强制编译器执行此操作:


{

...

addColumn(新CColumn(TIMESTAMP_COLUMN,this,CString( TIMESTAMP)));

...

}


另一种解决方法是引入第三个构造函数

需要一个常量字符*

-

Karl Heinz Buchegger
kb ****** @ gascad.at




" Ron Natalie" ; < ro*@sensor.com>在消息中写道

news:40 ********************* @ news.newshosting.com。 ..

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40*********************@news.newshosting.com. ..

乔 < JR ***** @ purina.com>写在留言
news:ec ************************** @ posting.google.c om ...

"Joe" <jr*****@purina.com> wrote in message news:ec**************************@posting.google.c om...
当调用CDual()构造函数中的下一行时,带有CColumn :: CColumn签名的
构造函数(CString columnType,
CObject * aOwner ,bool batchUpdated)被调用,而不是我想要的那个。
When the line below in the CDual() constructor gets called, the
constructor with the signature of CColumn::CColumn(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.



因为bool比chartring更适合char *。任何



Because the bool is a better match for char* than CString is. Any



指针都可以转换为bool,这是一个标准的转换序列。
我假设CString有一个带有char *的转换构造函数。这是用户定义的转换序列。标准转换序列
胜出。


pointer can be converted to bool, and that is a standard conversion sequence.
I assume CString has a converting constructor that takes a char*. This
is a user-defined conversion sequence. The standard conversion sequence
wins out.




这是语言的一部分,是标准的一部分,还是编译器

依赖,你推断'这是他的实施?



Is this true for the language as part of the standard, or is it compiler
dependent and you''re inferring that''s his implementation?


这篇关于错误的构造函数被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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