473,498 Members | 1,648 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(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
Jul 22 '05 #1
17 1646

"Joe" <jr*****@purina.com> wrote in message news:ec**************************@posting.google.c om...
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.


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"));

Jul 22 '05 #2
Joe wrote:


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?


Simple. Force the compiler to do it:

{
...
addColumn(new CColumn(TIMESTAMP_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*********************@news.newshosting.com. ..

"Joe" <jr*****@purina.com> wrote in message news:ec**************************@posting.google.c om...
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.


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.google.c om...
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);
...
}
[SNIP implementation details]
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

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_COLUMN, this, CString ("TIMESTAMP"));
2) Create a new constructor CColumn (CString columnType,CObject *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******@gascad.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.net...
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******@gascad.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.google.c om...
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.net...

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

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

news:ec**************************@posting.google.c om...
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.


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

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

"jeffc" <no****@nowhere.com> wrote in message

news:40********@news1.prserv.net...
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.


Whaddya know. Personally I don't think that conversion should be there - it
seems meaningless for a literal string.
Jul 22 '05 #11

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

Whaddya know. Personally I don't think that conversion should be there - it
seems meaningless for a literal string.

What conversion are you talking about? First off, it's pure conjecture what CString
does because it's not a standard type. I'm just assuming it has a constructor of the
form CString(const char*). Second, string literals don't make a string. They make
a character array. It's braindamage carried over from C. char* sucks as a string
type.

Jul 22 '05 #12

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

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

"jeffc" <no****@nowhere.com> wrote in message news:40********@news1.prserv.net...
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.


Whaddya know. Personally I don't think that conversion should be there -

it seems meaningless for a literal string.

It's because a string literal is treated as a const char array, and a
pointer to that array is created and used. The conversion is then really
from a char* to a bool, which makes a great deal of sense, since it is
common to check for a non-null pointer by writing something like

if (pointer_to_something){......}
-Howard

Jul 22 '05 #13

"Howard" <al*****@hotmail.com> wrote in message news:bu********@dispatch.concentric.net...
It's because a string literal is treated as a const char array,

That's because it IS a const char array.

Jul 22 '05 #14

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

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


Whaddya know. Personally I don't think that conversion should be there - it seems meaningless for a literal string.

What conversion are you talking about? First off, it's pure conjecture

what CString does because it's not a standard type. I'm just assuming it has a constructor of the form CString(const char*). Second, string literals don't make a string. They make a character array. It's braindamage carried over from C. char* sucks as a string type.


You're quite correct, but I was talking about a string literal to bool.
It's a silly conversion, virtually certain to be a mistake.
Jul 22 '05 #15

"Howard" <al*****@hotmail.com> wrote in message
news:bu********@dispatch.concentric.net...

Whaddya know. Personally I don't think that conversion should be
there - it
seems meaningless for a literal string.

It's because a string literal is treated as a const char array, and a
pointer to that array is created and used. The conversion is then really
from a char* to a bool, which makes a great deal of sense, since it is
common to check for a non-null pointer by writing something like

if (pointer_to_something){......}


Yeah, I understand your point. It just seems to me that when a compiler can
distinguish between a literal and a pointer, it should. To put it another
way, I don't see why a string literal has to blindly be treated like a
pointer in all cases. Keep in mind that bools aren't implied in your
example. For example,
if ("asdf")
is one thing, because it doesn't imply conversion to bool. Forcing
conversion to bool specifically is another thing.
Jul 22 '05 #16

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

Yeah, I understand your point. It just seems to me that when a compiler can
distinguish between a literal and a pointer, it should. To put it another
way, I don't see why a string literal has to blindly be treated like a
pointer in all cases.


Well the real culprit is the array-to-pointer translation. Frankly, I think that
was a mistake. If people wanted a pointer to the first element, they should
be forced to do something like &x[0]. That and arrays should work like
all the other data types:
int x[3] = { 0, 1, 2};
int y[3];

y = x;
etc...

Jul 22 '05 #17

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

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

Yeah, I understand your point. It just seems to me that when a compiler can distinguish between a literal and a pointer, it should. To put it another way, I don't see why a string literal has to blindly be treated like a
pointer in all cases.


Well the real culprit is the array-to-pointer translation. Frankly, I

think that was a mistake. If people wanted a pointer to the first element, they should be forced to do something like &x[0].


I also don't see any problem with that.
Jul 22 '05 #18

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

Similar topics

10
3378
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...
3
4527
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
4
1594
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...
6
2543
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...
4
1718
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
3712
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...
1
3505
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...
12
7169
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? ...
16
3404
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),...
0
7126
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,...
0
7005
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...
1
6891
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...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
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...
0
1424
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 ...
0
293
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...

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.