Help: Function Overloading Resolution, Which Function?

Discussion in 'C++' started by coolpint@yahoo.co.uk, Mar 27, 2006.

  1. Guest

    I am yet again confused about the process of overload resolution.

    Can anyone kindly explain why the function #2 is selected over #1 when
    passed 'char[15]'?

    When passed 'const char[15]', the compiler complains of ambiguity as I
    expected.
    I was expecting to hear the same complaint when passing 'char[15]'
    because I thought both required 'const qualification', but my compiler
    (gcc 3.4.2) calls function #2.

    So I definitely must be misunderstanding something. Please help me
    understand what is going on. Thank you so much in advance!

    #include <iostream>
    using std::cout;

    void which(const char* a, const char* b) { // #1
    cout << "const char * ";
    }

    void which(const char (&)[15], const char (&)[15]) { // #2
    cout << "const char (&)[15] ";
    }

    int main()
    {
    const char a[15] = {"Whatever"};
    const char b[15] = {"Whatever"};
    // which(a,b); // overload resolution failure as expected...

    char c[15];
    char d[15];
    which(c,d); // Why does it select the "const char (&)[15]" ?
    return 0;
    }
    , Mar 27, 2006
    #1
    1. Advertising

  2. Guest

    I think that in"const char a[15]" a is a type of const char* or const
    array, the para of two functions is ok.
    In "char a[15]" a is a type of char* or array.
    Do you think a type of "char*" can conceal to a type of "const char*"?
    Change the #1 below, "which(c,d)" will be failure.
    Example:
    void which(char* const a, char* const b) { // #1
    cout << "const char * ";



    } //So the char* can conceal to char* const
    , Mar 27, 2006
    #2
    1. Advertising

  3. Guest

    by the way what does "const char (&)[15]" mean?
    , Mar 27, 2006
    #3
  4. Tomás Guest

    posted:

    > by the way what does "const char (&)[15]" mean?
    >
    >


    A const reference to an array of fifteen "char"'s.

    -Tomás
    Tomás, Mar 27, 2006
    #4
  5. Marcus Kwok Guest

    > posted:
    >> by the way what does "const char (&)[15]" mean?
    >>
    >>


    "Tom?s" <> wrote:
    > A const reference to an array of fifteen "char"'s.


    Actually, it is a reference to an array of fifteen "const char"s. The
    "const" applies to the chars, so they cannot be changed. All references
    are const since they cannot be reseated, so saying a "const reference"
    is redundant.

    --
    Marcus Kwok
    Marcus Kwok, Mar 27, 2006
    #5
  6. Guest

    (0) Then how can I give it a name? because "const char (&)[15] array"
    does not work
    and I need a name, for example I want to ask what is its 4th element (
    array[3] )
    (1) And why I need the semicolons (I need because it does not compile
    without them)?
    |'m looking forward the answer. thanks
    , Mar 27, 2006
    #6
  7. Marcus Kwok Guest

    Hi bonczz,
    First, please read http://cfaj.freeshell.org/google/ to see how you
    should quote context in your replies.


    Going back to the original code:

    <> wrote:
    > #include <iostream>
    > using std::cout;
    >
    > void which(const char* a, const char* b) { // #1
    > cout << "const char * ";
    > }
    >
    > void which(const char (&array)[15], const char (&)[15]) { // #2
    > cout << "const char (&)[15] ";
    > }
    >
    > int main()
    > {
    > const char a[15] = {"Whatever"};
    > const char b[15] = {"Whatever"};
    > // which(a,b); // overload resolution failure as expected...
    >
    > char c[15];
    > char d[15];
    > which(c,d); // Why does it select the "const char (&)[15]" ?
    > return 0;
    > }


    Using Visual Studio .NET 2003, which is also known as
    Visual C++ .NET 7.1, I get the following compiler error:

    test.cpp
    test.cpp(20) : error C2668: 'which' : ambiguous call to overloaded function
    test.cpp(8): could be 'void which(const char (&)[15],const char (&)[15]) '
    test.cpp(4): or 'void which(const char *,const char *)'
    while trying to match the argument list '(char [15], char [15])'

    However, Comeau online compiles it. Someone who has better knowledge of
    the function overload resolution rules will have to elaborate.

    <> wrote:
    > (0) Then how can I give it a name? because "const char (&)[15] array"
    > does not work
    > and I need a name, for example I want to ask what is its 4th element (
    > array[3] )


    You can say:

    const char (&array)[15]

    > (1) And why I need the semicolons (I need because it does not compile
    > without them)?


    I'm not sure which semicolons you are talking about. C++ needs a lot of
    semicolons :)

    --
    Marcus Kwok
    Marcus Kwok, Mar 27, 2006
    #7
    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. Andrew FPGA
    Replies:
    0
    Views:
    969
    Andrew FPGA
    Sep 26, 2005
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,404
    Fredrik Lundh
    Sep 30, 2005
  3. Fredrik Lundh
    Replies:
    0
    Views:
    448
    Fredrik Lundh
    Sep 30, 2005
  4. Steve Holden
    Replies:
    0
    Views:
    425
    Steve Holden
    Sep 30, 2005
  5. Iyer, Prasad C
    Replies:
    4
    Views:
    575
    John J. Lee
    Sep 30, 2005
Loading...

Share This Page