Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Interesting parameter overload prob...

Reply
Thread Tools

Interesting parameter overload prob...

 
 
Noah Roberts
Guest
Posts: n/a
 
      10-10-2003
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

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-10-2003

"Noah Roberts" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-10-2003
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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-10-2003
"Mike Wahler" <(E-Mail Removed)> wrote in message
news:7EFhb.5145$(E-Mail Removed) 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


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      10-11-2003
Mike Wahler wrote:
> "Noah Roberts" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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

 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      10-11-2003
Noah Roberts wrote:

> Mike Wahler wrote:
>
>> "Noah Roberts" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>
>>> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unresolved overload type in parameter to std::random_shuffle Juha Nieminen C++ 1 12-02-2011 06:57 PM
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Interesting overload case David Sachs C++ 8 11-27-2003 07:33 PM



Advertisments