Why a member function will override an free function with the samename even if the arguments does no

Discussion in 'C++' started by Peng Yu, Sep 19, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    The following code doesn't work at the commented line. I'm wondering
    why C++ grammar is defined this way. Shouldn't C++ be smarter enough
    to figure out whether an member function or a free function shall be
    used?

    Thanks,
    Peng

    #include <iostream>

    struct A {
    A(int a) : _a(a) { }
    int _a;
    };


    void doit(const A &a) {
    std::cout << a._a << std::endl;
    }

    struct B {
    public:
    B(const A &a) : _a(a) { }
    void doit() {
    doit(_a);// the member function is conflicted with the free
    function.
    }
    private:
    A _a;
    };


    int main () {
    B b((A(2)));
    b.doit();
    }
     
    Peng Yu, Sep 19, 2008
    #1
    1. Advertising

  2. Peng Yu

    peter koch Guest

    Re: Why a member function will override an free function with thesame name even if the arguments does not match?

    On 19 Sep., 23:22, Peng Yu <> wrote:
    > Hi,
    >
    > The following code doesn't work at the commented line. I'm wondering
    > why C++ grammar is defined this way. Shouldn't C++ be smarter enough
    > to figure out whether an member function or a free function shall be
    > used?
    >
    > Thanks,
    > Peng
    >
    > #include <iostream>
    >
    > struct A {
    >     A(int a) : _a(a) { }
    >     int _a;
    >
    > };
    >
    > void doit(const A &a) {
    >   std::cout << a._a << std::endl;
    >
    > }
    >
    > struct B {
    >   public:
    >     B(const A &a) : _a(a) { }
    >     void doit() {
    >       doit(_a);// the member function is conflicted with the free
    > function.
    >     }
    >   private:
    >     A _a;
    >
    > };
    >
    > int main () {
    >   B b((A(2)));
    >   b.doit();


    Because the standard says so. Actually, I also believe that this is a
    good solution. The alternative would be to look all possible functions
    up and choose the best candidate, but that easily becomes obscure in
    some cases.
    If you want to call a global function, all you have to do is to scope
    the name as (here) ::doit.

    /Peter
     
    peter koch, Sep 19, 2008
    #2
    1. Advertising

  3. Peng Yu

    James Kanze Guest

    Re: Why a member function will override an free function with thesame name even if the arguments does not match?

    On Sep 19, 11:59 pm, peter koch <> wrote:
    > On 19 Sep., 23:22, Peng Yu <> wrote:
    > > The following code doesn't work at the commented line. I'm
    > > wondering why C++ grammar is defined this way. Shouldn't C++
    > > be smarter enough to figure out whether an member function
    > > or a free function shall be used?


    > > #include <iostream>


    > > struct A {
    > > A(int a) : _a(a) { }
    > > int _a;
    > > };


    > > void doit(const A &a) {
    > > std::cout << a._a << std::endl;
    > > }


    > > struct B {
    > > public:
    > > B(const A &a) : _a(a) { }
    > > void doit() {
    > > doit(_a);// the member function is conflicted with the free
    > > function.
    > > }
    > > private:
    > > A _a;
    > > };


    > > int main () {
    > > B b((A(2)));
    > > b.doit();


    > Because the standard says so. Actually, I also believe that
    > this is a good solution. The alternative would be to look all
    > possible functions up and choose the best candidate, but that
    > easily becomes obscure in some cases.


    More than obscure, it's fragile. Someone adds an unrelated
    function to a header file, and suddenly, your code unexplainedly
    stops working. Presumably, if you have provided a member
    function, that's the one you want to call. (I'm not even sure
    that ADL kicks in if there is a member function. I rather hope
    not.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 20, 2008
    #3
  4. Peng Yu

    Salt_Peter Guest

    Re: Why a member function will override an free function with thesame name even if the arguments does not match?

    On Sep 19, 5:22 pm, Peng Yu <> wrote:
    > Hi,
    >
    > The following code doesn't work at the commented line. I'm wondering
    > why C++ grammar is defined this way. Shouldn't C++ be smarter enough
    > to figure out whether an member function or a free function shall be
    > used?


    Absolutely not, name lookup is strictly defined by the standard in
    such a way that ambiguity and loose lookups are not allowed (it gets
    quite complicated / involved too). The point here is that regardless
    which compiler, architecture or platform you run the program in, no
    loose interpretations of the standard causes side-effects or varied
    results.
    Keep it simple and secure. That *is* smarter.
    Case in point, you have a program with 10K lines of code, once you
    learn how to decypher name lookup diagnostics, correcting the issue is
    childs play. With loose lookups: forget it. You would probably not
    even be aware that in fact an error occurred.

    >
    > Thanks,
    > Peng
    >
    > #include <iostream>
    >
    > struct A {
    > A(int a) : _a(a) { }
    > int _a;
    >
    > };
    >
    > void doit(const A &a) {
    > std::cout << a._a << std::endl;
    >
    > }
    >
    > struct B {
    > public:
    > B(const A &a) : _a(a) { }
    > void doit() {
    > doit(_a);// the member function is conflicted with the free
    > function.
    > }
    > private:
    > A _a;
    >
    > };
    >
    > int main () {
    > B b((A(2)));
    > b.doit();
    >
    > }
     
    Salt_Peter, Sep 20, 2008
    #4
  5. Peng Yu

    peter koch Guest

    Re: Why a member function will override an free function with thesame name even if the arguments does not match?

    On 20 Sep., 10:20, James Kanze <> wrote:
    > On Sep 19, 11:59 pm, peter koch <> wrote:
    >
    >
    >
    >
    >
    > > On 19 Sep., 23:22, Peng Yu <> wrote:
    > > > The following code doesn't work at the commented line. I'm
    > > > wondering why C++ grammar is defined this way. Shouldn't C++
    > > > be smarter enough to figure out whether an member function
    > > > or a free function shall be used?
    > > > #include <iostream>
    > > > struct A {
    > > >     A(int a) : _a(a) { }
    > > >     int _a;
    > > > };
    > > > void doit(const A &a) {
    > > >   std::cout << a._a << std::endl;
    > > > }
    > > > struct B {
    > > >   public:
    > > >     B(const A &a) : _a(a) { }
    > > >     void doit() {
    > > >       doit(_a);// the member function is conflicted with the free
    > > > function.
    > > >     }
    > > >   private:
    > > >     A _a;
    > > > };
    > > > int main () {
    > > >   B b((A(2)));
    > > >   b.doit();

    > > Because the standard says so. Actually, I also believe that
    > > this is a good solution. The alternative would be to look all
    > > possible functions up and choose the best candidate, but that
    > > easily becomes obscure in some cases.

    >
    > More than obscure, it's fragile.  Someone adds an unrelated
    > function to a header file, and suddenly, your code unexplainedly
    > stops working.  Presumably, if you have provided a member
    > function, that's the one you want to call.  


    And if you don't, it is very easy to tell the compiler otherwise. This
    is a very good example.

    > (I'm not even sure
    > that ADL kicks in if there is a member function.  I rather hope
    > not.)


    I believe not, but as I have recently changed pc, I don't have my
    draft standard ready.

    /Peter
     
    peter koch, Sep 20, 2008
    #5
    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. Stan Goodman

    Even older fart, even newer newbie

    Stan Goodman, Jul 3, 2003, in forum: Java
    Replies:
    11
    Views:
    709
    Stan Goodman
    Jul 4, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    921
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  4. Hamish
    Replies:
    3
    Views:
    589
    Alf P. Steinbach
    Jan 25, 2008
  5. lightning
    Replies:
    4
    Views:
    1,299
    lightning
    Apr 3, 2008
Loading...

Share This Page