Connecting Tech Pros Worldwide Forums | Help | Site Map

How to determine if a virtual method has been overridden?

wink
Guest
 
Posts: n/a
#1: Jul 11 '08
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


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 11 '08

re: How to determine if a virtual method has been overridden?


wink wrote:
Quote:
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 [...]
So, the replies of two experts who frequent this newsgroup are not
enough for you, are they?

Let me ask you this: why do you think you need to know whether the
function has or hasn't been overridden?
Quote:
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Jul 11 '08

re: How to determine if a virtual method has been overridden?


On 2008-07-11 22:00, wink wrote:
Quote:
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
wink
Guest
 
Posts: n/a
#4: Jul 14 '08

re: How to determine if a virtual method has been overridden?


On Jul 11, 1:08*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
wink wrote:
Quote:
I'd like to determine if a method has been overridden as was asked
here:
>>
Quote:
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.
Quote:
>
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.
Quote:
>
Quote:
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
Paavo Helde
Guest
 
Posts: n/a
#5: Jul 14 '08

re: How to determine if a virtual method has been overridden?


wink <wink@saville.comkirjutas:
Quote:
I'd like to determine if a method has been overridden as was asked
here:
So, you need a dynamic dispatch based on the object type. The virtual
function system of C++ was invented to do just that, so it is a solution,
not a problem ;-)

If this solution does not match your needs, you can always fall back to C
style of implementing OOP, with more control over the implementation
details, see below. But such a need should not arise often, if ever, so
most probably whatever problem you have can be resolved in a better way.

hth
Paavo

#include <iostream>

class B;
class A;

void m_for_b(B* this_ptr) {
std::cout << "in m_for_b()\n";
}

void m_for_a(B* this_ptr) {
std::cout << "in m_for_a()\n";
}

class B {
public:
B():m_(&m_for_b) {}
~B() {}
typedef void (*m_t)(B*);
m_t m_;
};

class A : public B {
public:
A() {m_ = &m_for_a;}
~A() {}
};


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

int main() {
A a;
B b;

test(b);
test(a);
}





Quote:
http://www.velocityreviews.com/forum...-whether-a-der
ived-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
>
Greg Herlihy
Guest
 
Posts: n/a
#6: Jul 14 '08

re: How to determine if a virtual method has been overridden?


On Jul 11, 1:00*pm, wink <w...@saville.comwrote:
Quote:
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

wink
Guest
 
Posts: n/a
#7: Jul 14 '08

re: How to determine if a virtual method has been overridden?


On Jul 14, 5:52*am, Greg Herlihy <gre...@mac.comwrote:
Quote:
On Jul 11, 1:00*pm, wink <w...@saville.comwrote:
>
>
>
Quote:
I'd like to determine if a method has been overridden as was asked
here:
>>
Quote:
The answer was can't do it, but I thought I'd ask here, my test code
is:
>
Quote:
...
>
Quote:
The compiler (gcc 4.0.3) complains:
>
Quote:
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'
>
Quote:
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
Closed Thread