Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to determine if a virtual method has been overridden?

Reply
Thread Tools

How to determine if a virtual method has been overridden?

 
 
wink
Guest
Posts: n/a
 
      07-11-2008
I'd like to determine if a method has been overridden as was asked
here:

http://www.velocityreviews.com/forum...rfunction.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

 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      07-11-2008
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/forum...rfunction.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
 
Reply With Quote
 
 
 
 
wink
Guest
Posts: n/a
 
      07-13-2008
On Jul 11, 1:08*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> wink wrote:
> > I'd like to determine if a method has been overridden as was asked
> > here:

>
> >http://www.velocityreviews.com/forum...ng-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
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      07-14-2008
On Jul 11, 1:00*pm, wink <w...@saville.com> wrote:
> I'd like to determine if a method has been overridden as was asked
> here:
>
> http://www.velocityreviews.com/forum...ng-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

 
Reply With Quote
 
wink
Guest
Posts: n/a
 
      07-14-2008
On Jul 14, 5:52*am, Greg Herlihy <gre...@mac.com> wrote:
> On Jul 11, 1:00*pm, wink <w...@saville.com> wrote:
>
>
>
> > I'd like to determine if a method has been overridden as was asked
> > here:

>
> >http://www.velocityreviews.com/forum...ng-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
DataGridView - Determine if a row has been edited =?Utf-8?B?UCBL?= ASP .Net 1 04-20-2006 05:08 PM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
Determine whether scripting has been disabled in browser Roz Lee ASP .Net 0 04-06-2005 04:54 AM
nuby: determine method passed and determine the receiver that received the method Peña, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57