473,396 Members | 2,102 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,396 software developers and data experts.

Determining whether a derived class overrides a virtual memberfunction

I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.

Neil
Dec 28 '07 #1
4 1618
On Dec 27, 5:17 pm, lord trousers <neil.toro...@gmail.comwrote:
I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.

Neil
Funny you should post this a few minutes before me... I'm waiting for
the same answer. Let me know if you figure anything out?
Dec 28 '07 #2
On 2007-12-28 02:17, lord trousers wrote:
I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.
I do not think you can do that in C++, and there are two reasons for
that: the language was designed to hide this kind of stuff from the
users, and this sounds like implementation specific territory, so even
if you find something that seems to work it might not on another
compiler or in the next version of your current.

You could use templates to get to know the actual type of the object,
but that seems undesirable, and I am not sure even that would work. If
you could get the member function pointer for the object it might quite
possibly be just an index into the vtable, and what you really want to
compare is the values in the entries those indexes points to. But since
there is no way in C++ to access the vtables you can not do that either.

#include <iostream>

struct B
{
virtual void bar() { }
};

struct D1 : public B
{
virtual void bar() { }
};

struct D2 : public B
{
virtual void bar() { }
};

int main()
{
std::cout << &D1::bar << std::endl;
std::cout << &D2::bar << std::endl;
}

--
Erik Wikström
Dec 28 '07 #3
lord trousers <ne**********@gmail.comwrote in news:9100a745-2328-479a-
ae***************@e10g2000prf.googlegroups.com:
I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
Are you sure the "if" check would cost less than a virtual function call?

If yes, then AFAIK what you want can't be done in the language. I guess
you could do it non-portably for each platform/implementation you
support. If you only have single inheritance, then it should be
relatively simple: the vtable pointer is usually in the beginning of
Object, with the knowledge about which vtable slot holds your finalize()
function pointer you could be able to compare the pointers.

However, I strongly suggest to avoid this path; if the virtual finalize()
call is costing too much there is probably something wrong in the overall
design, maybe an attempt to handle each integer in an array as a separate
object?

HTH
Paavo
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.

Neil
Jan 9 '08 #4
On Dec 28 2007, 2:17 am, lord trousers <neil.toro...@gmail.comwrote:
I'm implementing a garbage collector in C++ for a fun new
language (don't ask), and I've found that it spends a good
amount of time calling "finalize" on objects that are
freed/not relocated. The default implementation does nothing
and always will, and most types won't need to override it.
It'd be nice if I could test for overrides, but the obvious
thing doesn't work:
class Object {
// ...
virtual void finalize() { }
// ...
};
// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();
// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'
I'd love to do what it says, but I don't know the type of the
object. Is there an easy way to make this same test? I want
to avoid RTTI and compiler-specific language extensions, and
also make it externally transparent.
Let me see if I understand: you want to avoid RTTI, but you want
RTTI. As soon as you try to find out something about the
dynamic type, you need RTTI---generally, a virtual function is
cheaper (in runtime) than anything else.

With regards to finalization with garbage collection, the usual
solution is to require explicit registration. This means a
(very slight) amount of extra work for the programmer who needs
finalization (but such cases are rare), but also means that you
don't have to derive everything from a common base---garbage
collection can also work for arrays of char, or what have you.
(This is how the Boehm collector works, but of course, it also
works with C, where virtual functions aren't an option.)

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 10 '08 #5

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

Similar topics

7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
1
by: Jing You | last post by:
hi every one, I have got some confused problem when I try to write some custom object by javascript. Look at the example code here: <BODY> <script language="jscript">
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
4
by: Akhil | last post by:
Hi All, Can u please explain this. Base Obj = new Derived(); Can Obj access methods both of Base and Derived or what will be the behaviour? What will be the behaviour for Overridden...
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
3
by: dbuchanan | last post by:
Can inherited code call derived code? If so how. I have identical 'generic' code that I am repeating again and again in several derived form because I don't know how to get inherited code to call...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
8
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.