473,626 Members | 3,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrong Constructor Called

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(dlle xport)CColumn : public CColumnBase
{
public:

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

The implementation of these functions looks like this...

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

CColumn::CColum n(CString columnType, CObject *aOwner, bool
batchUpdated)
{
setColumnType(c olumnType);
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::CColum n(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCR EATE( CDual, CDomain )

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

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

Thanks,

Joe
Jul 22 '05 #1
17 1662

"Joe" <jr*****@purina .com> wrote in message news:ec******** *************** ***@posting.goo gle.com...
When the line below in the CDual() constructor gets called, the
constructor with the signature of CColumn::CColum n(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.


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(TIMESTA MP_COLUMN, this, CString("TIMEST AMP"));

Jul 22 '05 #2
Joe wrote:


CDual::CDual()
{
...
addColumn(new CColumn(TIMESTA MP_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?


Simple. Force the compiler to do it:

{
...
addColumn(new CColumn(TIMESTA MP_COLUMN,this, CString( "TIMESTAMP" )));
...
}

another workaround would be to introduce a third constructor which
takes a const char*
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3

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

"Joe" <jr*****@purina .com> wrote in message news:ec******** *************** ***@posting.goo gle.com...
When the line below in the CDual() constructor gets called, the
constructor with the signature of CColumn::CColum n(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.


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.


Is this true for the language as part of the standard, or is it compiler
dependent and you're inferring that's his implementation?
Jul 22 '05 #4
"Joe" <jr*****@purina .com> wrote in message
news:ec******** *************** ***@posting.goo gle.com...
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(dlle xport)CColumn : public CColumnBase
{
public:

CColumn(CString columnType,CObj ect *aOwner, CString anId);
CColumn(CString columnType,CObj ect *aOwner, bool batchUpdated);
...
}
[SNIP implementation details]
CDual::CDual()
{
...
addColumn(new CColumn(TIMESTA MP_COLUMN,this, "TIMESTAMP" ));
...
}

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

Thanks,

Joe

It's because you aren't calling the constructor with a (CString) - you are
calling it with "TIMESTAMP" which is a (char const*). There is a
language-defined conversion from a pointer to a bool (non-NULL -> true,
NULL -> false), which beats out the user-defined conversion from (char
const*) to (CString). I don't remember the exact language rule here, but
it's come up in my code before. You have several options:

1) Instead call CColumn (TIMESTAMP_COLU MN, this, CString ("TIMESTAMP" ));
2) Create a new constructor CColumn (CString columnType,CObj ect *aOwner,
char const *anId);
3) Change the argument orders, or add an argument to a constructor.
4) Use the named constructor idiom. (static CColumn *createById (...);
static CColumn *createBatch (...).
5) Passing a bool to a constructor is often a hint to break the class into
two, using polymorphism.

What you do depends on several factors, including what code is yours. I
don't recall if CColumn is an MFC class or not.

HTH
--
KCS


Jul 22 '05 #5

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:40******** *******@gascad. at...
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.


It's not a bug. The conversion of a pointer to bool is a standard
conversion sequence. The conversion to CString is a user-defined
conversion. A standard conversion sequence is preferred over a
user-defined conversion. That's the language.

Jul 22 '05 #6

"jeffc" <no****@nowhere .com> wrote in message news:40******** @news1.prserv.n et...
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's the way the C++ language works. There's nothing wrong
with his compiler in this regard.

Jul 22 '05 #7
Ron Natalie wrote:

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:40******** *******@gascad. at...
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.


It's not a bug. The conversion of a pointer to bool is a standard
conversion sequence. The conversion to CString is a user-defined
conversion. A standard conversion sequence is preferred over a
user-defined conversion. That's the language.

Thank's for clearification.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #8

"Joe" <jr*****@purina .com> wrote in message
news:ec******** *************** ***@posting.goo gle.com...
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...
[SNIP] Does anyone have any idea why this is happening and how to avoid this
issue?


This is a problem of the conversion sequence. The standard states that the
order of conversions are 1. standard-conversion, 2. user defined
conversions, 3. elipsis conversions. The conversion to bool is a valid
standard conversion for any pointer as it is indicated in section 4.12 of
the standard. CString has a ctor enable implicit user conversions for string
literals. However, the standard conversion to bool wins due to the ordering.
What you have to do is to disamiguate the ctor call by supplying a CString
object as the 3rd parameter or provide another ctor which can take a const
char*.

Regards
Chris
Jul 22 '05 #9

"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...

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

"Joe" <jr*****@purina .com> wrote in message

news:ec******** *************** ***@posting.goo gle.com...
When the line below in the CDual() constructor gets called, the
constructor with the signature of CColumn::CColum n(CString columnType,
CObject *aOwner, bool batchUpdated) gets invoked, rather than the one
I want.


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.


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's defined by the standard in section 13.3.3.1 (ISO:IEC 14882:1998(E))

Chris
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3397
by: Greener | last post by:
Hi, I need help badly. Can you do client-side programming instead of server-side to capture the Browser type info? If this is the case, what's wrong with the following? <script language="JavaScript"> function doWord(file) { if (navigator.userAgent.indexOf("MSIE")!=-1)
3
4553
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
4
1618
by: Kench | last post by:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1 hour ago still it does not show up there so posting this here. Hi, Consider class A & B both of which implement a copy constructor. class B inherits from A. When copy constructor of B is called, first the constructor of A gets called My question is that, why the copy constructor for A not called. Is there a way to have it call the copy constructor of class A?
6
2563
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
4
1725
by: Bas van der Veer | last post by:
Hi, if I put the line: var T = new Array(-1); anywhere in my script, it stops working. e.g <SCRIPT LANGUAGE="JAVASCRIPT"> <!--
7
3729
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
1
3524
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
12
7191
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? class Trial { public: Trial() {
16
3425
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName), _netguid(netguid) {}
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.