473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to determine if a virtual method has been overridden?

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

Jul 11 '08 #1
6 5705
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 [...]
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?
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
Jul 11 '08 #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/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
Jul 11 '08 #3
On Jul 11, 1:08*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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
Jul 13 '08 #4
wink <wi**@saville.comkirjutas:
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);
}

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
Jul 14 '08 #5
On Jul 11, 1:00*pm, wink <w...@saville.comwrote:
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

Jul 14 '08 #6
On Jul 14, 5:52*am, Greg Herlihy <gre...@mac.comwrote:
On Jul 11, 1:00*pm, wink <w...@saville.comwrote:
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
Jul 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
11
by: S. I. Becker | last post by:
Is it possible to determine if a function has been overridden by an object, when I have a pointer to that object as it's base class (which is abstract)? The reason I want to do this is that I want...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.