How to determine if a virtual method has been overridden?

Discussion in 'C++' started by wink, Jul 11, 2008.

  1. wink

    wink Guest

    I'd like to determine if a method has been overridden as was asked
    here:

    http://www.velocityreviews.com/foru...class-overrides-a-virtual-memberfunction.html

    The answer was can't do it, but I thought I'd ask here, my test code
    is:

    #include <iostream>

    class B {
    public:
    B() {}
    ~B() {}
    virtual void m() {}
    };

    class A : public B {
    public:
    A() {}
    ~A() {}
    virtual void m() {}
    };


    void test(const B &r) {
    if (&r.m == &B::m) {
    // do something not overridden
    std::cout << "not overridden" << std::endl;
    } else {
    // do something else was overridden
    std::cout << "overridden" << std::endl;
    }
    }

    int main(int argc, char *argv[]) {
    A a;
    B b;

    test(b);
    test(a);
    return 0;
    }




    The compiler (gcc 4.0.3) complains:

    tor.cc: In function 'void test(const B&)':
    tor.cc:22: error: ISO C++ forbids taking the address of a bound member
    function to form a pointer to member function. Say '&B::m'

    Any suggestions on how this can be done?

    Thanks

    -- Wink Saville
     
    wink, Jul 11, 2008
    #1
    1. Advertising

  2. On 2008-07-11 22:00, wink wrote:
    > I'd like to determine if a method has been overridden as was asked
    > here:
    >
    > http://www.velocityreviews.com/foru...class-overrides-a-virtual-memberfunction.html
    >
    > The answer was can't do it, but I thought I'd ask here, my test code
    > is:
    >
    > #include <iostream>
    >
    > class B {
    > public:
    > B() {}
    > ~B() {}
    > virtual void m() {}
    > };
    >
    > class A : public B {
    > public:
    > A() {}
    > ~A() {}
    > virtual void m() {}
    > };
    >
    >
    > void test(const B &r) {
    > if (&r.m == &B::m) {
    > // do something not overridden
    > std::cout << "not overridden" << std::endl;
    > } else {
    > // do something else was overridden
    > std::cout << "overridden" << std::endl;
    > }
    > }
    >
    > int main(int argc, char *argv[]) {
    > A a;
    > B b;
    >
    > test(b);
    > test(a);
    > return 0;
    > }
    >
    >
    >
    >
    > The compiler (gcc 4.0.3) complains:
    >
    > tor.cc: In function 'void test(const B&)':
    > tor.cc:22: error: ISO C++ forbids taking the address of a bound member
    > function to form a pointer to member function. Say '&B::m'
    >
    > Any suggestions on how this can be done?


    Even if your code worked you would probably only get an index into the
    objects vtable, which will be the same for both the base class and the
    derived class.

    --
    Erik Wikström
     
    Erik Wikström, Jul 11, 2008
    #2
    1. Advertising

  3. wink

    wink Guest

    On Jul 11, 1:08 pm, Victor Bazarov <> wrote:
    > wink wrote:
    > > I'd like to determine if a method has been overridden as was asked
    > > here:

    >
    > >http://www.velocityreviews.com/forums/t564224-determining-whether-a-d...

    >
    > > The answer was can't do it, but I thought I'd ask here [...]

    >
    > So, the replies of two experts who frequent this newsgroup are not
    > enough for you, are they?


    Sorry didn't realize it was an authoritative response.

    >
    > Let me ask you this: why do you think you need to know whether the
    > function has or hasn't been overridden?


    I was writing test code and didn't want to do the test if
    the method wasn't overridden.

    >
    > > Any suggestions on how this can be done?

    >
    > No, not really.  The language does not have the mechanism probably
    > because it is not necessary in a normal course of C++ programming.


    So I guess if it becomes really important I'll have the author
    of the derived calls communicate the information to the test code
    directly.

    Thanks,

    -- Wink
     
    wink, Jul 14, 2008
    #3
  4. wink

    Greg Herlihy Guest

    On Jul 11, 1:00 pm, wink <> wrote:
    > I'd like to determine if a method has been overridden as was asked
    > here:
    >
    > http://www.velocityreviews.com/forums/t564224-determining-whether-a-d...
    >
    > The answer was can't do it, but I thought I'd ask here, my test code
    > is:
    >
    >...
    >
    > The compiler (gcc 4.0.3) complains:
    >
    > tor.cc: In function 'void test(const B&)':
    > tor.cc:22: error: ISO C++ forbids taking the address of a bound member
    > function to form a pointer to member function.  Say '&B::m'
    >
    > Any suggestions on how this can be done?


    The g++ compiler does allow taking the address of a bound member
    function as a C++ language extension. Essentially, the C++ program
    must be compiled with a "-Wno-pmf-conversions" flag. There is also
    some unusual syntax required to obtain the address of the bound member
    pointer. For details, see this comp.lang.c++.moderated post of mine:

    http://preview.tinyurl.com/6s3skt

    Greg
     
    Greg Herlihy, Jul 14, 2008
    #4
  5. wink

    wink Guest

    On Jul 14, 5:52 am, Greg Herlihy <> wrote:
    > On Jul 11, 1:00 pm, wink <> wrote:
    >
    >
    >
    > > I'd like to determine if a method has been overridden as was asked
    > > here:

    >
    > >http://www.velocityreviews.com/forums/t564224-determining-whether-a-d...

    >
    > > The answer was can't do it, but I thought I'd ask here, my test code
    > > is:

    >
    > >...

    >
    > > The compiler (gcc 4.0.3) complains:

    >
    > > tor.cc: In function 'void test(const B&)':
    > > tor.cc:22: error: ISO C++ forbids taking the address of a bound member
    > > function to form a pointer to member function.  Say '&B::m'

    >
    > > Any suggestions on how this can be done?

    >
    > The g++ compiler does allow taking the address of a bound member
    > function as a C++ language extension. Essentially, the C++ program
    > must be compiled with a "-Wno-pmf-conversions" flag. There is also
    > some unusual syntax required to obtain the address of the bound member
    > pointer. For details, see this comp.lang.c++.moderated post of mine:
    >
    >      http://preview.tinyurl.com/6s3skt
    >
    > Greg


    It worked, I wonder what other compilers
    support this or similar extension?

    Thanks,

    -- Wink
     
    wink, Jul 14, 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. Patel
    Replies:
    6
    Views:
    40,767
    Randall R Schulz
    Oct 23, 2003
  2. Patel
    Replies:
    1
    Views:
    436
    Harald Hein
    Oct 23, 2003
  3. S. I. Becker
    Replies:
    11
    Views:
    557
    Earl Purple
    Jun 14, 2006
  4. Bruno Desthuilliers
    Replies:
    12
    Views:
    506
    Raymond Hettinger
    Sep 24, 2007
  5. Peña, Botp
    Replies:
    1
    Views:
    261
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page