Interesting parameter overload prob...

N

Noah Roberts

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
 
M

Mike Wahler

Noah Roberts said:
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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
M

Mike Wahler

Mike Wahler said:
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
 
N

Noah Roberts

Mike said:
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
 
L

lilburne

Noah said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top