473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Interesting parameter overload prob...

EmailAccount(std::string key_name, bool editable = true);
EmailAccount(std::string account_manager_key, std::string
account_key, bool editable = true);

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002"); // Calls 1st constructor, not
2nd like it should.
EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002", true); // acts as expected.

Is this a programmer or compiler malfunction?

NR

Jul 19 '05 #1
5 1548

"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vo************@corp.supernews.com...
EmailAccount(std::string key_name, bool editable = true);
EmailAccount(std::string account_manager_key, std::string
account_key, bool editable = true);

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002"); // Calls 1st constructor, not
2nd like it should.
The "00000002" (type const char*) is being "silently"
converted to type 'bool'. (my compiler gives a warning).

EmailAccount testAccount(my_key + "blah" + std::string("00000002");

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002", true); // acts as expected.

Is this a programmer or compiler malfunction?


Um, no comment. :-)

-Mike
Jul 19 '05 #2
Noah Roberts escribió:
EmailAccount(std::string key_name, bool editable = true);
EmailAccount(std::string account_manager_key, std::string
account_key, bool editable = true);

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002"); // Calls 1st constructor, not
2nd like it should.

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002", true); // acts as expected.


Conversion from const char * to bool takes precedence over constructing
a std::string, according to overload resolution rules.

You can use std::string ("00000002"), or add another EmailAccount
constructor that takes a const char * as second parameter.

Regards.
Jul 19 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:7E*****************@newsread3.news.pas.earthl ink.net...
The "00000002" (type const char*) is being "silently"
converted to type 'bool'. (my compiler gives a warning).

EmailAccount testAccount(my_key + "blah" + std::string("00000002");


Oops,

EmailAccount testAccount(my_key + "blah", std::string("00000002");

-Mike
Jul 19 '05 #4
Mike Wahler wrote:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vo************@corp.supernews.com...
EmailAccount(std::string key_name, bool editable = true);
EmailAccount(std::string account_manager_key, std::string
account_key, bool editable = true);

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002"); // Calls 1st constructor, not
2nd like it should.

The "00000002" (type const char*) is being "silently"
converted to type 'bool'. (my compiler gives a warning).


Ahh, so it is being interpreted as a non-null pointer, which is true.
Should have expected that, live and learn.

NR

Jul 19 '05 #5
Noah Roberts wrote:
Mike Wahler wrote:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vo************@corp.supernews.com...
EmailAccount(std::string key_name, bool editable = true);
EmailAccount(std::string account_manager_key, std::string
account_key, bool editable = true);

EmailAccount testAccount(my_key + "\\Software\\Microsoft\\Internet
Account Manager\\Accounts\\", "00000002"); // Calls 1st constructor, not
2nd like it should.


The "00000002" (type const char*) is being "silently"
converted to type 'bool'. (my compiler gives a warning).

Ahh, so it is being interpreted as a non-null pointer, which is true.
Should have expected that, live and learn.


Beware of default parameters, they are seductive sirens that
will lead you and your clients astray.

There are two main reasons for using them:

1) You find at some later date that you need to add an extra
argument and can't be bothered to do the trawl that an API
change would involve.

2) You want your method to behave differently if the extra
parameter is supplied. IOW you want to disguise the fact
that one method is actually two, or more methods, and save
clients programmers a few keystrokes.

1 is sometimes justified but 2 never is.

In the case given I believe reason 2 is involved.
Personally, I'd be upfront about the fact that instances of
your class can be either editable or not and redesigned it
somewhat like:

enum EmailAccountEditable {
Edit,
NoEdit
};

EmailAccount(std::string key_name, EmailAccountEditable
editable);
EmailAccount(std::string account_manager_key, std::string
account_key, EmailAccountEditable editable);

later when some one has to read the code the won't have to
worry about whether:

EmailAccount account(key, NoEdit);

is creating an editable instance of the class or not.

With default parameters people reading code will often miss
the subtlety of a missing parameter in a call.

Jul 19 '05 #6

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

Similar topics

5
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts....
8
by: David Sachs | last post by:
The following program illustrates an interesting effect of the way C++ resolves function overloading. I have verified with a member of the C++ stardard committee that the output shown is...
9
by: Ryan Taylor | last post by:
Hello. I am trying to overload a method so that I have four possible working copies. The only difference between the four methods is what to search by and in what manner to return the results....
5
by: Lasse Edsvik | last post by:
Hello I have this: public SqlParameter GetParameter(string sqlparam) { SqlParameter param = cmd.Parameters.Add(sqlparam);
5
by: MS | last post by:
Here's my simple stored procedure: ALTER PROCEDURE GetMemberIDByEmail @Email EmailAddress, @ID int OUTPUT AS SELECT @ID = ID FROM tbl_Member WHERE Email=@Email RETURN
3
by: Dean Slindee | last post by:
Is there a way to replace the commented out parameter add syntax with a single line of code, like the .Parameter.Add("@Letter"...) line below (which does not work)? 'Dim prmLetter As New...
5
by: Andreas Mueller | last post by:
Hi All, I have an overloaded generic method anmed "Foo": Class Test Class Xox(Of T) End Class Class Lulli(Of T) Inherits Xox(Of T) End Class
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: Gary Wessle | last post by:
I have a method with the last arguments having defaults like this void filing(long, double, const myType& r_mo=NULL, const herType& r_tr=NULL); is that acceptable, if not how is it done?...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.