How to determine if a virtual method has been overridden?

W

wink

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
 
E

Erik Wikström

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.
 
W

wink

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

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.
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
 
G

Greg Herlihy

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
 
W

wink

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top