Interesting parameter overload prob...

Discussion in 'C++' started by Noah Roberts, Oct 10, 2003.

  1. Noah Roberts

    Noah Roberts Guest

    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
    Noah Roberts, Oct 10, 2003
    #1
    1. Advertising

  2. Noah Roberts

    Mike Wahler Guest

    "Noah Roberts" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 10, 2003
    #2
    1. Advertising

  3. 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.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 10, 2003
    #3
  4. Noah Roberts

    Mike Wahler Guest

    "Mike Wahler" <> wrote in message
    news:7EFhb.5145$...
    > 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
    Mike Wahler, Oct 10, 2003
    #4
  5. Noah Roberts

    Noah Roberts Guest

    Mike Wahler wrote:
    > "Noah Roberts" <> wrote in message
    > news:...
    >
    >> 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
    Noah Roberts, Oct 11, 2003
    #5
  6. Noah Roberts

    lilburne Guest

    Noah Roberts wrote:

    > Mike Wahler wrote:
    >
    >> "Noah Roberts" <> wrote in message
    >> news:...
    >>
    >>> 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.
    lilburne, Oct 11, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Sachs

    Interesting overload case

    David Sachs, Nov 26, 2003, in forum: C++
    Replies:
    8
    Views:
    353
    Andrey Tarasevich
    Nov 27, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    371
    Nick Hounsome
    Jan 25, 2004
  3. Replies:
    4
    Views:
    519
    Matteo
    Mar 29, 2007
  4. Juha Nieminen
    Replies:
    1
    Views:
    380
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    251
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page