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.