473,657 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::fin alize)
pobj->finalize();

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

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 1633
On Dec 27, 5:17 pm, lord trousers <neil.toro...@g mail.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::fin alize)
pobj->finalize();

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

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::fin alize)
pobj->finalize();

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

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**********@g mail.comwrote in news:9100a745-2328-479a-
ae************* **@e10g2000prf. googlegroups.co m:
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::fin alize)
pobj->finalize();

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

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...@g mail.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::fin alize)
pobj->finalize();
// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::fi nalize'
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 objektorientier ter Datenverarbeitu ng
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
1882
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 destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be virtual always. What exactly is the reason for not having it so?
1
3525
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
3317
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
4
4332
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 Methods?
10
1390
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 Derived and New it as Derived the code in the Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in
3
1504
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 derived code. Am I stuck with this situation or is there a way around it? Below is some sample code. ============================== Private Sub LoadDataInForm() '= Form_Load Call FillDataSet() Call CreateBindings() Call...
2
2634
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 class BaseClass {
8
2010
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 of its own, maybe specifying the liquor store over on 44th Street and 5th Avenue or something. Anyway this is what we have so far: base class: "foo" |------------method: "rob_the_liquor_store()" |
10
4084
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.